$(function() { $('.add-script').click(function() { $('body').append('<script class="gmaps-script" src="https://maps.googleapis.com/maps/api/js?key=MYKEY&callback=init"></script>'); }); $('.remove-script').click(function() { // Remove previously appended script tag here }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button class="add-script">Load Map</button> <button class="remove-script">Remove Map</button>
您可以使用删除最后一个脚本标记
$('script').last().remove();
id="gmaps-js" 然后用 $('#gmaps-js').remove()
id="gmaps-js"
$('#gmaps-js').remove()