document.write()工作内联但不使用jQuery的.html()

时间:2013-03-21 18:25:44

标签: javascript jquery document.write

如果页面加载时我的dom中已经有document.write,它只会写入要写入的位置。例如:

<div id="d1">
existing text <script>document.write('& doc written text');</script>
</div>
<div>footer</div>

将呈现为<div id="d1">existing text & doc written text</div><div>footer</div>

但是,如果我使用jQuery来更新div中的html,比如

$("#d1").html("another approach <script>document.write('wipes out the screen');</script>");

我最终只使用wipes out the screen b / c它基本上摆脱了文档中的所有内容,而不仅仅是内联脚本所在的位置。我如何.html()使用包含document.write()的值,但其行为与在dom中开始时的行为相同(请参阅第一个示例)。谢谢!

2 个答案:

答案 0 :(得分:3)

如果页面已完全写入,则无法使用document.write()。你无法改变它的行为方式。你需要重新思考你在做什么。

答案 1 :(得分:3)

html()函数替换元素的内容。 (See the API)

你想要的是append()

相关问题