问题
I have a problem with my jQuery. So, I want to remove a script who is inside a <div>
.
My html is :
<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 :
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
回答1:
You can specify it as the following code:
html.find('script[src="http://firstScript.com"]').remove();
回答2:
Assuming the <script>
tags are actually in your html DOM and you have jQuery reference included.
You can use .filter()
to get the script
with src
as 'http://firstScript.com'
and the .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 Not allowed and would throw error Unexpected Token Illegal
回答3:
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();
});
FIDDLE
回答4:
The only need changes
var html = $(stringOfHtml);
html.remove();
回答5:
To select and remove your first script you can do this way : $('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;
回答6:
$(document).ready(function(){
$('.right-bloc-pub-1').html().remove();
});
try this
来源:https://stackoverflow.com/questions/30072887/remove-a-script-with-jquery