Javascript document.write()可以内联工作,但不使用jQuery';s.html()
如果页面加载时dom中已经有Javascript document.write()可以内联工作,但不使用jQuery';s.html(),javascript,jquery,document.write,Javascript,Jquery,Document.write,如果页面加载时dom中已经有document.write,那么它只在调用写入的地方写入。例如: <div id="d1"> existing text <script>document.write('& doc written text');</script> </div> <div>footer</div> 现有文本文档。写入(“&doc书面文本”); 页脚 将呈现为现有文本和文档写入文本页脚 但是,如果我使
document.write
,那么它只在调用写入的地方写入。例如:
<div id="d1">
existing text <script>document.write('& doc written text');</script>
</div>
<div>footer</div>
现有文本文档。写入(“&doc书面文本”);
页脚
将呈现为现有文本和文档写入文本页脚
但是,如果我使用jQuery来更新div中的html,比如
$("#d1").html("another approach <script>document.write('wipes out the screen');</script>");
$(“#d1”).html(“另一个方法文档.write('wipe out the screen');”);
我的最终结果是,
清除屏幕
b/c它基本上清除了文档中的所有内容,而不是只在内联脚本所在的位置编写。如何使用包含document.write()
的值.html()
,但使其行为与dom中的行为相同(请参见第一个示例)。谢谢 如果页面已完全写入,则不能使用document.write()。你无法改变它的行为方式。你需要重新思考你在做什么。函数替换元素的内容
您需要的是。加载DOM后不能使用document.write()
,否则它将擦除整个文档。你想这样使用它有什么原因吗?没什么document.write=function(){alert(“hello world”});}
:)@Paisle是的,您可以覆盖document.write,但我希望看到您在呈现页面时将文本放在相同的位置。完美的答案,这正是我们必须做的-后退后重新编写整个内容<代码>文档。我们已经了解到,编写
可能很危险!(而且强大)