使用jquery删除脚本

我的jquery有问题。 所以,我想删除一个位于

内的脚本。 我的HTML是:

 

我的jquery:

  var stringOfHtml = ''; var html = $(stringOfHtml); html.find('script').remove(); 

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

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

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

假设标签实际上在你的html DOM中并且你包含了jQuery引用。

您可以使用.filter()srcscript作为'http://firstScript.com'.remove()

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

也,

 var stringOfHtml = ''; var html = $(stringOfHtml); 

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

唯一需要改变

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

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

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

试试这个:

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

要么

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

小提琴

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

试试这个