2016-06-16 11 views
-2

ここに私のプレーンなHTMLコードがあります。<style>タグと<script>タグを、プレーンHTMLからJqueryを使用して削除します。

<style> 
/* style tag goes here*/ 
</style> 
<form method="POST" action="#" id="_form_93_" class="_form _form_93 _inline-form _dark" novalidate> 
    <input type="hidden" name="u" value="93" /> 
    <input type="hidden" name="f" value="93" /> 
    <input type="hidden" name="s" /> 
    <input type="hidden" name="c" value="0" /> 
    <input type="hidden" name="m" value="0" /> 
</form> 
<script type="text/javascript"> 
// script tag goes here 
</script> 

私はそう期待される結果がされるHTMLコードから<style></style><script></script>タグを削除します。

<form method="POST" action="#" id="_form_93_" class="_form _form_93 _inline-form _dark" novalidate> 
    <input type="hidden" name="u" value="93" /> 
    <input type="hidden" name="f" value="93" /> 
    <input type="hidden" name="s" /> 
    <input type="hidden" name="c" value="0" /> 
    <input type="hidden" name="m" value="0" /> 
</form> 
+0

なぜですか? 'css'と' script'を書かないでください! – Rayon

+1

http://api.jquery.com/remove –

+0

@Rayon実際にはこれは自動応答のhtmlコードです。私はちょうど

コードです。 –

答えて

2

あなたはこの

$('style, script').remove(); 

スタイルを使用してタグを削除することができますが、もはや画面上に表示されませんし、両方のHTML要素をDOMから消えるだろう。

でも、remove()コードを実行する前にイベントが添付されていれば、JavaScriptコードは実行されます。

(たとえば)ajax呼び出しで返されたコードを消去するだけの場合は、正常に動作します。あなたがすでに入っているページからスタイルとJavaScriptをクリアしたい場合は、scriptタグを削除する前に、いくつかのJavaScriptコードがすでに要素にアタッチされている可能性があります。

Check this fiddle例です。平野はJavaScriptを使用して

jQuery('style').remove(); 

jQuery('script').remove(); 

:jQueryの使用

0

window.onload = function(){ 
    var getStyle = document.getElementsByTagName("style"); 
    getStyle[0].remove(); 
    var getScript = document.getElementsByTagName("script"); 
    getScript[0].remove(); 
} 

をH1を削除する方法を示しており、作業フィドルを見つけてください。タグ、Link

関連する問題