How to export source content within div to text/html file

前端 未结 4 1428
醉酒成梦
醉酒成梦 2020-12-03 03:44

Let\'s say I have

... Some html ...
and I need to take the html code within this div, place it inside a file and for
4条回答
  •  不思量自难忘°
    2020-12-03 04:34

    Update for Zagen function. Original will not work in IE 10-11:

    function downloadInnerHtml(filename, elId, mimeType) {
        var elHtml = document.getElementById(elId).innerHTML;
        if (navigator.msSaveBlob) { // IE 10+ 
            navigator.msSaveBlob(new Blob([elHtml], { type: mimeType + ';charset=utf-8;' }), filename);
        } else {
            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(); 
        }
    }
    

提交回复
热议问题