且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从页面动态删除脚本

更新时间:2023-12-05 18:15:46

您必须给脚本一个ID.

you have to give the script an id.

$("#runMyCode").click(function(){
   $("#resultContainer").html($("#htmlTextArea").val());
   var newScript = document.createElement('script');
   newScript.id = "goback_script";
   var newTextNode=document.createTextNode($("#jsTextArea").val());
   newScript.type = 'text/javascript';
   newScript.appendChild(newTextNode);
   document.body.appendChild(newScript);
   var newStyle = document.createElement('style');
   var newTextNode2=document.createTextNode($("#cssTextArea").val());
   newStyle.type = 'text/css';
   newStyle.appendChild(newTextNode2);
   document.body.appendChild(newStyle);
});
$('#goBack').click(function(){
   var script = document.getElementById("goback_script");
   script.parentElement.removeChild(script);
});

jsFiddle