[英]Dynamically remove all content between between script tags of a html file
Suppose, in my project homepage there are some javascript code. 假设在我的项目主页中有一些javascript代码。 For example,
例如,
index.html 的index.html
<body>
html code here
<script type="text/javascript">
(function () {
some javascript code here
})();
</script>
<button type="button">Delete javascript</button>
</body>
Now, what I want is that, on clicking on the button all content between script tags should be deleted along with script tags. 现在,我想要的是,单击按钮时,脚本标记之间的所有内容都应与脚本标记一起删除。 How can I do that?
我怎样才能做到这一点?
Could you try putting an ID or Class on your script tag and then onlick find that ID or Class and remove it? 您能否尝试将ID或类放在您的脚本标签上,然后onlick找到该ID或类并将其删除? Something like:
就像是:
$('button').on('click', function() {
$('#script-tag-id').remove();
});
Try this: 尝试这个:
window.addEventListener('load', function() { alert(document.body.innerHTML) document.getElementById('button').onclick = function() { if(document.getElementById('yourScript')){ var script = document.getElementById('yourScript'); document.body.removeChild(script); } alert(document.body.innerHTML); }; });
<body> html code here <script id="yourScript" type="text/javascript"> /*testing*/ </script> <button id="button" type="button">Delete javascript</button> </body>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.