2015-12-06 53 views
1

假設在我的項目主頁中有一些javascript代碼。例如,動態刪除html文件的腳本標記之間的所有內容

的index.html

<body> 
    html code here 

    <script type="text/javascript"> 
     (function() { 

    some javascript code here 

     })(); 
    </script> 


<button type="button">Delete javascript</button> 
    </body> 

現在,我想的是,在點擊按鈕腳本標記之間的所有內容應與腳本標記一起被刪除。我怎樣才能做到這一點?

+0

你知道刪除腳本標籤不會影響它的代碼執行?你爲什麼要刪除它? – undefined

+0

'document.body.removeChild(yourScriptElem);' – www139

回答

1

你可以嘗試在腳本標記上放一個ID或類,然後onlick找到該ID或類並將其刪除?喜歡的東西:

$('button').on('click', function() { 
    $('#script-tag-id').remove(); 
}); 
1

試試這個:

window.addEventListener('load', function() { 
 
    alert(document.body.innerHTML) 
 
    document.getElementById('button').onclick = function() { 
 
    if(document.getElementById('yourScript')){ 
 
     var script = document.getElementById('yourScript'); 
 
     document.body.removeChild(script); 
 
    } 
 
    alert(document.body.innerHTML); 
 
    }; 
 
});
<body> 
 
    html code here 
 
    <script id="yourScript" type="text/javascript"> 
 
    /*testing*/ 
 
    </script> 
 
    <button id="button" type="button">Delete javascript</button> 
 
</body>

相關問題