jquery 删除 javascript 代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8799322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 04:36:33  来源:igfitidea点击:

jquery remove javascript code

javascriptjquery

提问by Soumalya Banerjee

I am able to remove html element using jquery using $(element).remove();. But there is a javascript code, like setinterval(), which should be removed. Yes, I can do clearInterval()for particular that case, but what if other types of javascript code?

我可以使用 jquery 删除 html 元素$(element).remove();。但是有一个 javascript 代码,比如setinterval(),应该删除。是的,我可以clearInterval()针对特定情况做,但如果是其他类型的 javascript 代码呢?

     <script>
     var baseurl = '...';
     jQuery(function(){
        new AjaxUpload('#upload_button', {
           action: baseurl,
           onSubmit : function(file , ext){
              jQuery('#...').val(file);
           },
           onComplete: function(file, response){
              // do something....;
           }
        });
     });
     </script>

But I am unable to do so.

但我无法这样做。

Can anybody help me out this problem?

有人可以帮我解决这个问题吗?

回答by Chamika Sandamal

set a id to your script,

为您的脚本设置一个 id,

<script id="sample">
...
</script>

and use this code,

并使用此代码,

$("#sample").remove();

回答by Gaurav Gupta

I'm not sure what exactly you're trying to achieve but in general, removing Javascript code from the document is not the correct way to "switch off" certain functionality you have enabled via Javascript.

我不确定您究竟想要实现什么,但总的来说,从文档中删除 Javascript 代码并不是“关闭”您通过 Javascript 启用的某些功能的正确方法。

For example, if you have set event handlers on certain links and want to undo them, you should use the offfunction. Similarly, there would be a way to "undo" everything you've added.

例如,如果您在某些链接上设置了事件处理程序并想要撤消它们,您应该使用off函数。同样,有一种方法可以“撤消”您添加的所有内容。

回答by Oleg

There is a way to wrap response in div element and remove all script tags,

有一种方法可以在 div 元素中包装响应并删除所有脚本标签,

var el = $('<div>'+response+'</div>');
$('script', el).remove();
response = el.html();