HTML button to save div content using javascript

前端 未结 4 916
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-03 05:45

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

jsfiddle

4条回答
  •  旧巷少年郎
    2021-01-03 06:34

    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
    }
    

    Working Fiddle

提交回复
热议问题