【发布时间】:2018-02-23 15:41:30
【问题描述】:
我正在尝试运行此代码以从我的 HTML 代码中的段落中获取元素并显示:
<!DOCTYPE html>
<html>
<body>
<p>This is a p element<br>
This is also a p element.<br>
This is also a p element - Click the button to change the background color of all p elements in this document.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
win = window.open();
win.document.open();
var x = document.getElementsByTagName("p");
for (var i = 0; i < x.length; i++) {
win.document.write(document.getElementById(x.item(i).id).innerHTML);
}
}
</script>
</body>
</html>
我不知道怎么做,但我总是得到一个空白窗口。有什么帮助吗?
谢谢。
【问题讨论】:
-
document.write写在页面上,你永远不想使用它,因为它很糟糕而且过时了 -
@SterlingArcher — 在没有文档的新窗口中调用
document.write。问题是文档是blank,而不是现有内容被新内容覆盖。 -
@SterlingArcher 正确使用,有很多/一些可能的应用(例如stackoverflow.com/a/1014251/402037)
标签: javascript html