[英]Reload div with script tag inside
I have a page where I want to reload the content of a div with a script tag inside. 我有一个页面,我想在其中重新加载带有脚本标签的div内容。 The code I use to reload this div works almost fine except that the script tag inside the div is stripped out after I reload the div.
我用来重新加载该div的代码几乎可以正常工作,除了在我重新加载div之后删除了div中的script标签。
In the following code, I have tried to explain my situation 在下面的代码中,我试图解释我的情况
<div id="myDiv">
<?php echo date('Y-m-d H:i:s', time()); ?>
<script>
document.write("Script");
</script>
</div>
<a onclick="reloadDiv();">Reload div</a>
<script>
function reloadDiv(){
$("#myDiv").load(location.href+" #myDiv");
}
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
So when I press Reload div the date updates but the script disappears 因此,当我按Reload div时,日期会更新,但脚本会消失
The code I use on my site is different but has the same principle. 我在网站上使用的代码不同,但原理相同。 On my site, I use PHP to add an ID from a database so my script has to be in that div.
在我的网站上,我使用PHP从数据库添加ID,因此我的脚本必须位于该div中。
You are replacing the whole content of your div when the load call returns. 当load调用返回时,您将替换div的全部内容。 You should move your script tag somewhere else, eg in the head tag of your document.
您应该将脚本标签移动到其他位置,例如在文档的标头中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.