久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何將 div 中的源內(nèi)容導(dǎo)出到 text/html 文件

How to export source content within div to text/html file(如何將 div 中的源內(nèi)容導(dǎo)出到 text/html 文件)
本文介紹了如何將 div 中的源內(nèi)容導(dǎo)出到 text/html 文件的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

限時(shí)送ChatGPT賬號(hào)..

假設(shè)我有

... 一些 html ...</div> 我需要在這個(gè) div 中獲取 html 代碼,將它放在一個(gè)文件中并強(qiáng)制以 TXT 格式下載它.

這怎么辦?我應(yīng)該使用 PHP 還是 JavaScript?我更喜歡 JavaScript.

解決方案

你可以這樣使用:

更新了 jsfiddle,下載 btn(jquery)

使用普通 js 和自動(dòng)執(zhí)行的初始 jsfiddle

html

<span>你好</span>

html - 編輯(添加了執(zhí)行此操作的鏈接)

Js

function downloadInnerHtml(filename, elId, mimeType) {var elHtml = document.getElementById(elId).innerHTML;var link = document.createElement('a');mimeType = mimeType ||'文本/普通';link.setAttribute('下載', 文件名);link.setAttribute('href', 'data:' + mimeType + ';charset=utf-8,' + encodeURIComponent(elHtml));鏈接.點(diǎn)擊();}var fileName = 'tags.html';//如果需要,您可以使用 .txt 擴(kuò)展名

JS - 編輯

既然您在評(píng)論中說(shuō)您使用的是 jQuery,我將從處理程序中調(diào)用此函數(shù),而不是直接調(diào)用它.

$('#downloadLink').click(function(){downloadInnerHtml(fileName, 'main','text/html');});

您可以以文本形式下載,只需刪除函數(shù)的第三個(gè)參數(shù),它將采用默認(rèn)值text/plain",并在文件名中添加擴(kuò)展名 .txt 而不是 html.

注意我編輯了這個(gè)答案,因?yàn)樘釂?wèn)的人評(píng)論說(shuō)他正在尋找如何讓它與處理程序一起工作,他讓它工作,但以防萬(wàn)一.原代碼在jsfiddle

Let's say I have <div id="main"> ... Some html ...</div> and I need to take the html code within this div, place it inside a file and force a download of it in TXT format.

How can that be done? Should I use PHP or JavaScript? I would prefer JavaScript. 解決方案 You could use something like this: Updated jsfiddle with download btn(jquery) Initial jsfiddle with plain js and autoexecution html<div id="main"> <span>Hey there</span> </div> html - Edit (Added a link to perform this action)<a href="#" id="downloadLink">Download the inner html</a> Jsfunction downloadInnerHtml(filename, elId, mimeType) { var elHtml = document.getElementById(elId).innerHTML; var link = document.createElement('a'); mimeType = mimeType || 'text/plain'; link.setAttribute('download', filename); link.setAttribute('href', 'data:' + mimeType + ';charset=utf-8,' + encodeURIComponent(elHtml)); link.click(); } var fileName = 'tags.html'; // You can use the .txt extension if you want JS - Edit Since you said in the comments that you are using jQuery i'll call this function from a handler, instead of calling it directly.$('#downloadLink').click(function(){ downloadInnerHtml(fileName, 'main','text/html'); }); You can download as a text, just remove the third argument for function, and it will take the default which is "text/plain", and add the extension .txt to the filename instead of html. Note I edited this answer since the person who asked commented that he was looking how to make it work with a handler, he made it work, but just in case. The original code is in the jsfiddle 這篇關(guān)于如何將 div 中的源內(nèi)容導(dǎo)出到 text/html 文件的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)! 【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Add programmatically a downloadable file to Woocommerce products(以編程方式將可下載文件添加到 Woocommerce 產(chǎn)品)
Get today#39;s total orders count for each product in Woocommerce(獲取今天 Woocommerce 中每種產(chǎn)品的總訂單數(shù))
Add Custom registration fields in WooCommerce and phone field validation issue(在 WooCommerce 和電話字段驗(yàn)證問(wèn)題中添加自定義注冊(cè)字段)
Add a select field that will change price in Woocommerce simple products(在 Woocommerce 簡(jiǎn)單產(chǎn)品中添加一個(gè)將更改價(jià)格的選擇字段)
Add custom columns to admin products list in WooCommerce 3(在 WooCommerce 3 中將自定義列添加到管理產(chǎn)品列表)
Customizing checkout quot;Place Orderquot; button output html(自定義結(jié)帳“下訂單按鈕輸出html)
主站蜘蛛池模板: 激情网五月天 | 久久久久国产精品一区二区 | 国产探花在线观看视频 | 久久国产亚洲 | 国产精品久久久久久久久久尿 | 怡红院成人在线视频 | 日韩在线欧美 | 亚洲精品乱码久久久久久按摩观 | 深夜爽视频 | 精品香蕉一区二区三区 | 超碰在线人人干 | av黄色免费在线观看 | 成人黄视频在线观看 | 免费视频一区二区三区在线观看 | 久久狼人天堂 | 99亚洲精品视频 | av在线播放网站 | 欧美一区二区三区在线观看 | 国产精品久久精品 | 国内自拍第一页 | 欧美a在线 | 中文字幕第7页 | 欧美日韩中文字幕 | 天天干天天干 | 久草热在线 | 国产乱码精品一品二品 | 久久久久国产精品一区 | 欧美v在线观看 | 99久久精品一区二区成人 | 成人精品在线观看 | 91av视频在线观看 | 97久久精品午夜一区二区 | 日韩在线观看网站 | 99色视频| 国产精彩视频在线观看 | 久久久久久久久中文字幕 | 国产精品久久久久一区二区 | www.jizzjizz| 午夜精品久久久久久久久久久久久 | 亚洲精品久久久久久久久久久久久 | 亚洲一区二区三区高清 |