簡體   English   中英

去掉<script> tags from a string along with the content between them

[英]Remove <script> tags from a string along with the content between them

我看了看,看了看,但我找不到任何東西。 所以我要說我有這個字符串......

var str = '<script>blah blah blah</script><a>...</a><div>...</div><p>...</p>';

我需要從字符串中刪除腳本標記以及標記之間的所有內容。 刪除腳本標記后,我需要將其附加到textarea。 我怎么用jQuery做這個呢?

任何幫助將非常感激!

因為沒有其他人會發布一個簡單而可靠的例程,我想我會:

function noscript(strCode){
   var html = $(strCode.bold()); 
   html.find('script').remove();
 return html.html();
}


alert(   noscript("<script>blah blah blah</script><div>blah blah blah</div>")    );
//shows: "<div>blah blah blah</div>"

嘗試這個:-

var str= "<script>blah blah blah</script><div>blah blah blah</div>";
var html = $(str.bold()); 
html.find('script').remove();

像這樣:

var div = $('<div>').html(str);

div.find('script').remove();

str = div.html();

示例: http//codepen.io/paulroub/pen/IabsE

這是一個小提琴( https://jsfiddle.net/skoley/9dnmbj3d/ )的答案,最好從其他幾個來源獲得,功勞歸於他們。

function noscript(strCode){
   var html = $(strCode.italics()); 
   html.find('script').remove();
 return html.html();
}

function noscript_alt(str)
{
var div = $('<div>').html(str);
div.find('script').remove();
str = div.html();
return str;}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM