2010-09-15 49 views
1

綜觀「In jQuery, want to remove all HTML inside of a div」,我竟能在正確的方向是什麼我正在尋找中刪除HTML 以下: 由數據庫驅動的文本大規模殺傷性武器的文本編輯器TEXTAREA

<textarea id="inputPane" cols="80" rows="40" class="pane" style="height:300px;"> 
<table><tr><td> 
<b>Since your service bureau</b>, xxx is not certified 
by our company for xyz, your company will need to complete 
more testing as part of the process. 
</td></tr></table> 
</textarea> 

使用從上面列出的URL這個代碼,我能明顯地移除所有的空格,但我無法刪除實際的HTML標籤:<b></b><table>

jQuery.fn.stripTags = function() 
{ return this.replaceWith(this.html().replace(/<\/?[^>]+>/gi, '')); }; 

這部分是什麼意思?

/<\/?[^>]+>/gi 

我覺得有這麼一句話:

  • 刪除此字符,<
  • 並刪除這個人物>
  • 和什麼也沒有更換。

雖然這不是它在我的代碼中所做的。它似乎從我放置的DIV中除去所有空白。

什麼是gi部件? 而且,當HTML顯示來自數據庫時,您是否有一個很好的解決方案來從wmd-editor textarea中移除HTML標記?

+0

'gi'意味着圖案全球應用,並忽略大小寫。 – 2012-12-03 14:16:22

回答

0

我用下面的腳本,以便從與字符串

function removeHTMLTags(htmlString) 
{ 
    if(htmlString) 
    { 
     var mydiv = document.createElement("div"); 
     mydiv.innerHTML = htmlString; 

     if (document.all) // IE Stuff 
     { 
      return mydiv.innerText; 
     } 
     else // Mozilla does not work with innerText 
     { 
      return mydiv.textContent; 
     }       
    } 
} 

不能完全確定刪除HTML標籤,但我認爲/gi部分表示全球在JavaScript取代像string.replace

2

我需要抓住一些div的div並將它們複製到textarea中。我把它全部寫成這樣一行:

$('#yourTextarea').val($('#yourTextarea').val()+$('.yourHTMLdivs').text().replace(/ +(?=)/g,'')); 

.yourHTMLdivs是被複制和剝離的部分。 .text()是去掉html的東西。最後的.replace()會帶出額外的空白。這樣做,您的信息將被全部剝離並設置爲在進入textarea之前。

(我在這裏工作了這一點,在計算器上讀三個四個類似的問題後,偉大的東西!)

-1

使用jQuery更簡單:

function removeHTMLTags(htmlString) 
{ 
    if(htmlString) 
    { 
     var mydiv = $("<div></div>"); 
     mydiv.html(htmlString); 
     retun mydiv.html(); 
    } 
} 
+0

錯字//更多字符 – user1596138 2015-02-13 19:10:40