2012-04-21 119 views
4

如何去除jQuery中的所有html標籤。在PHP喜歡用strip_tags功能用jQuery去掉所有的html標籤?

例如:有一些內容如下:

<div id='test'>This is <b>bold</b> and this <div>is</div> <i>italic</i>.</div> 

現在我想將其更改爲<div id='test'>This is bold and this is italic.</div>

+0

[Text HTML中的Strip HTML]的可能重複(http://stackoverflow.com/questions/822452/strip-html-from-text-javascript) – 2013-12-04 01:45:47

回答

11

使用.text()

$('#test').text($('#test').text()); 

我使用.text()兩次,因爲使用.html()設置內容將導致字符串小於和大於s igns被錯誤地呈現:

Hello &lt;foo&gt; // Set with .text() 
Hello <foo>  // Set with .html() 

如果使用.html(),然後<foo>將成爲一個標籤。