-1

我在尝试将标签放入 document.write() 调用时遇到问题。我认为这是因为 document.write() 已经存在于另一个标签中。

我在网上的某个地方看到还有一个 document.innerHTML() 调用。有谁知道这是否对我有帮助,如果没有,还有其他方法吗?

谢谢 :)

4

1 回答 1

2

document.write可用于在页面解析期间发出标记。解析后不能用于修改页面。的输出document.write直接进入解析器,就好像它一开始就在 HTML 文档中一样。例如:

<body>
<script>
document.write("<p>");
</script>
hi there</p>

浏览器中看起来与

<body>
<p>hi there</p>

innerHTML,它不是一个函数,而是一个属性,存在于所有 DOM 元素实例上,并且可以使用标记来设置它们的内容。这与实例上可用的各种 DOM 方法一起,是完成动态网页的主要方式。例如:

<body>
<p id="target">Hi there</p>
<script>
document.getElementById("target").innerHTML = "Updated by <strong>code</strong>";
</script>
</body>

...将段落从说“嗨”更改为“由代码更新”。

更多探索:

于 2012-07-27T13:22:50.953 回答