admin管理员组文章数量:1406937
If I have a document.write
already in my dom as the page loads, it writes only where it's called to write. For example:
<div id="d1">
existing text <script>document.write('& doc written text');</script>
</div>
<div>footer</div>
Will render as <div id="d1">existing text & doc written text</div><div>footer</div>
However, if I were to use jQuery to update the html within div, like
$("#d1").html("another approach <script>document.write('wipes out the screen');</script>");
I end up simply with wipes out the screen
b/c it essentially gets rid of everything in the document, vs. writing only where the inline script is. How can I .html()
with a value containing document.write()
, but have it behave the same as if it were in the dom to begin with (see first example). Thanks!
If I have a document.write
already in my dom as the page loads, it writes only where it's called to write. For example:
<div id="d1">
existing text <script>document.write('& doc written text');</script>
</div>
<div>footer</div>
Will render as <div id="d1">existing text & doc written text</div><div>footer</div>
However, if I were to use jQuery to update the html within div, like
$("#d1").html("another approach <script>document.write('wipes out the screen');</script>");
I end up simply with wipes out the screen
b/c it essentially gets rid of everything in the document, vs. writing only where the inline script is. How can I .html()
with a value containing document.write()
, but have it behave the same as if it were in the dom to begin with (see first example). Thanks!
-
1
You can't use
document.write()
after DOM is loaded, otherwise it will erase your entire document. Is there a reason you want to use it that way? – ulentini Commented Mar 21, 2013 at 18:28
2 Answers
Reset to default 4The html()
function replaces the contents of an element. (See the API)
What you want is append()
.
If the page has fully been written, you can not use document.write(). There is nothing you can do to change how it behaves. You need to rethink what you are doing.
本文标签: javascriptdocumentwrite() works inline but not using jQuery39s html()Stack Overflow
版权声明:本文标题:javascript - document.write() works inline but not using jQuery's .html() - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744971128a2635240.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论