【发布时间】:2017-08-31 22:55:49
【问题描述】:
我正在尝试加载一个新的脚本标记,该标记将 jQuery 内容(在 #clickmeDIV DIV 内)动态替换为新的,但是,一旦将第二个脚本标记加载到 #clickmeDIV DIV 中,第一个脚本即使它不是 DOM 的一部分,replaced 仍在运行。我通过使用 chrome 开发人员工具检查 DOM 确认了这一点。有什么方法可以完全擦除第一个脚本,然后动态替换为新脚本(无需重新加载页面)?
div {
width: 300px;
height: 300px;
background-color: blue;
}
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="clickmeDIV">
<script>
$(document).on('click', '#clickmeDIV', function() {
alert('Alert executed by the original script');
$('#clickmeDIV').empty();
$('#clickmeDIV').html('<script type="text/javascript">' +
'$(document).on("click","#clickmeDIV", function(){' +
'var foo = function(){alert("Alert executed by the script inserted dynamically"); }; foo();' +
'}); </s' + 'cript>');
});
</script>
</div>
【问题讨论】:
-
删除脚本无法撤销已执行代码所做的事情。
标签: javascript jquery html