Strip HTML from Text JavaScript

有没有一种简单的方法在JavaScript中取出一串html并去掉html?


If you're running in a browser, then the easiest way is just to let the browser do it for you...

function strip(html)
{
   var tmp = document.createElement("DIV");
   tmp.innerHTML = html;
   return tmp.textContent || tmp.innerText || "";
}

Note: as folks have noted in the comments, this is best avoided if you don't control the source of the HTML (for example, don't run this on anything that could've come from user input). For those scenarios, you can still let the browser do the work for you - see Saba's answer on using the now widely-available DOMParser.


myString.replace(/<(?:.|n)*?>/gm, '');

Simplest way:

jQuery(html).text();

That retrieves all the text from a string of html.

链接地址: http://www.djcxy.com/p/15358.html

上一篇: 使用<span>元素着色纯文本字符串

下一篇: 从文本JavaScript中去除HTML