使用 javascript 保存 div 内容的 HTML 按钮

2022-01-24 00:00:00 file button save javascript html

我需要使用纯 javascript 保存 div 的内容.我刚刚编辑了一个小提琴,但我无法使它工作:(

I need to save content of div using pure javascript. I just edited one fiddle but I can't make it works :(

jsfiddle

<div id="content">
<h1>Hello world</h1>
<i>Hi everybody</i>

下载

function download(){
    var a = document.body.appendChild(
        document.createElement("a")
    );
    a.download = "export.html";
    a.href = "data:text/html," + document.getElementById("content");
}

推荐答案

关闭,你需要innerHTML &也触发点击:

Close, you need innerHTML & trigger the click too:

function download(){
    var a = document.body.appendChild(
        document.createElement("a")
    );
    a.download = "export.html";
    a.href = "data:text/html," + document.getElementById("content").innerHTML; // Grab the HTML
    a.click(); // Trigger a click on the element
}

工作小提琴

相关文章