[英]jQuery update script tag - rewrite all content
我有jQuery的问题。 我只想将“ document.write”更改为src的链接。
我的代码:myscript.js
document.write("TEST ABCD");
test.html
<html>
<body>
<button id="example">click me</button>
<div>my site example text</div>
<div id="my_div_important">
Old test
</div>
<script>
$(function(){
$('#example').on('click',function(){
$('<script>').attr('src', 'http://example.com/script.js').appendTo('#my_div_important');
});
});
</script>
</body>
</html>
如果单击“ #example”,则在页面中写入:TEST ABCD,但其余所有内容都被隐藏... URL相同(无重定向)。
什么时候有问题?
-更新
我想做一个小部件,用户可以在其网站上使用它。
用户面板要预览不同大小的内容,因此需要更新脚本SRC标签脚本(example.com/widget/normal/250|300 itp。)
如果页面已经完全加载,则document.write会删除所有内容(就像对文档对象进行写操作一样)。
另外,您应该使用jQuery.getScript()来动态加载脚本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.