https://www.w3schools.com/js/tryit.asp?filename=tryjs_dom_image 这是关于用js改变html内容,但它是关于图像的。是否也可以做类似的事情,所以相同,但使用新的 html 文件进行操作,以便您可以在页面内切换页面? 因此计划是使用 js 将下面的 html(id="id")替换为另一个 html 文件。
document.getElementById("id").src = "otherfile.html";
<html>
<body>
hey guys! here is my html page. If you click underneath, content will be changed.
<html id="id">
<body>
here is the text that will change if id="id" will be changed by a different html file
</body>
</html>
</body>
</html>
当然,我知道这是行不通的。 但是有没有办法实现这个目标呢?
如果您正在阅读并尝试回答我的帖子,谢谢您,我知道这是一个困惑且糟糕的问题,所以向您表示感谢。
最佳答案
更改 div
的内容而不是整个 HTML
怎么样?通过 jQuery。
<div id="dynamic-html">
here is the text that will change if id="id" will be changed by a different html file
</div>
$( "#btnChangeHTML" ).click(function() {
$( "#dynamic-html" ).html("<div class='myclass'>This is the custom HTML</div>");
});
这是一个 fiddle :
关于javascript - 能用js切换html页面吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50532837/