通过JavaScript / jQuery将页面另存为HTML文件,包括新添加的元素

Asa*_*sad 1 html javascript jquery

我有一个Web应用程序,它通过appendChild()功能使用JavaScript添加HTML元素(如div)。当我使用Firebug检查(添加div之后)时,它显示了新添加的div。但是,当我在浏览器中看到源代码时,它并不能反映所做的更改。

我的问题是:如何使用JavaScript或jQuery将新添加的div以及其他HTML元素另存为HTML文件?


源代码:

<html>
<head>
<title>Page title</title>
<script src="https://code.jquery.com/jquery-2.2.3.min.js"></script>
<script>
 $(document).ready(function() {
  $('#btnAddtoList').click(function(){
    if( $('#inputAddtoList').val() == '' ) {alert("please enter some value."); }
    var text = $('#inputAddtoList').val();
    var newDiv = $('<div class="listing"><h4>' + text + '</h4></div>');
    $('body').append(newDiv);
    $('#inputAddtoList').val('');
   });
});
</script>
</head>
<body>

Enter Question: <BR/><textarea id="inputAddtoList" rows="5" cols="50" placeholder="Enter Question Here..." autofocus></textarea> 
<button id="btnAddtoList">Add Question</button>
 <BR /><BR />
<strong>Question :</strong>

</body>
</html>
Run Code Online (Sandbox Code Playgroud)

Ism*_*OUH 5

源代码是服务器发送到浏览器的内容,无法反映实时更改。Firebug帮助您检查实时DOM。通过使用jQuery,您可以获得页面中任何元素的HTML,包括整个页面$("html").html()。现代浏览器通过使用HTML5 W3C saveAs()函数和FileSaver.js作为polyfill支持保存文件:

var blob = new Blob([$("html").html()], {type: "text/html;charset=utf-8"});
saveAs(blob, "page.html");
Run Code Online (Sandbox Code Playgroud)

演示:https : //jsfiddle.net/iRbouh/dj5j3kLd/