javascript 使用 jQuery 删除脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30072887/
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-28 11:37:16  来源:igfitidea点击:

Remove a script with jQuery

javascriptjqueryjquery-pluginsjquery-events

提问by TanGio

I have a problem with my jQuery. So, I want to remove a script who is inside a <div>. My html is :

我的 jQuery 有问题。所以,我想删除一个在<div>. 我的 html 是:

<div id="right-bloc">
        <div class="right-bloc-pub-1">
            <script type="text/javascript" src="http://firstScript.com"></script>
        </div>
        <div class="right-bloc-pub-2">
            <script type="text/javascript" src="http://secondScript.com"></script>
        </div>
</div>

My jQuery :

我的jQuery:

 var stringOfHtml = '<script type="text/javascript" src="http://firstScript.com"></script>';
 var html = $(stringOfHtml);
 html.find('script').remove();

I want to remove the script witch contains http://firsttScript.com, but not working. It's possible to remove this script and add after? Because I need to refresh this script

我想删除女巫包含的脚本http://firsttScript.com,但不起作用。可以删除此脚本并在之后添加吗?因为我需要刷新这个脚本

回答by Radonirina Maminiaina

You can specify it as the following code:

您可以将其指定为以下代码:

html.find('script[src="http://firstScript.com"]').remove();

回答by Shaunak D

Assuming the <script>tags are actually in your html DOM and you have jQuery reference included.

假设<script>标签实际上在您的 html DOM 中,并且您包含了 jQuery 参考。

You can use .filter()to get the scriptwith srcas 'http://firstScript.com'and the .remove().

您可以使用.filter()来获取scriptwith srcas'http://firstScript.com'.remove()

$('html').find('script').filter(function(){
    return $(this).attr('src') === 'http://firstScript.com'
}).remove();


Also,

还,

var stringOfHtml = '<script type="text/javascript" src="http://firstScript.com"></script>';
var html = $(stringOfHtml);

This is Notallowed and would throw error Unexpected Token Illegal

这是不是允许的,将抛出错误Unexpected Token Illegal

回答by Saiyam Gambhir

Try This:

试试这个:

   $(document).ready(function(){
     $('.right-bloc-pub-1').find('script[src="http://firstScript.com"]').remove();
   });

OR

或者

$(document).ready(function(){
   $('.right-bloc-pub-1').find('script').attr('src', 'http://firstScript.com').remove();
});

回答by Ganbin

To select and remove your first script you can do this way : $('script[src="http://firstScript.com"]').remove()

要选择和删除您的第一个脚本,您可以这样做: $('script[src="http://firstScript.com"]').remove()

Just know that this will remove the script from your DOM but not the library loaded within this script. For example if your first script load jQuery, the $variable will still contains jQuery. To remove completely jQuery you can add $ = undefined; jQuery = undefined;

只需知道这将从您的 DOM 中删除脚本,但不会从该脚本中加载的库中删除。例如,如果您的第一个脚本加载 jQuery,则该$变量仍将包含 jQuery。要完全删除 jQuery,您可以添加$ = undefined; jQuery = undefined;

回答by VJP

$(document).ready(function(){
      $('.right-bloc-pub-1').html().remove();
});

try this

试试这个

回答by Vishwajeet Bose

The only need changes

唯一需要改变的

    var html = $(stringOfHtml);
    html.remove();