如何将HTML转换为没有标签和特殊字符的文本?

时间:2016-07-15 17:16:26

标签: javascript html string

我有一系列的引用如下:

<p>Being a graphic designer gets you used to rejection of your brilliance. So it&#8217;s good practice for dating.  </p>

我希望他们看起来像这样:

 Being a graphic designer gets you used to rejection of your brilliance. So it's good practice for dating.

我尝试使用innerHTML,但成效有限。

1 个答案:

答案 0 :(得分:2)

使用document.createElement方法创建一个临时span元素,其中html内容为字符串。稍后通过获取textContent属性来获取文本内容。

&#13;
&#13;
var str = '<p>Being a graphic designer gets you used to rejection of your brilliance. So it&#8217;s good practice for dating.  </p>';

// create a span element
var temp = document.createElement('span');
// set the html to the element
temp.innerHTML = str;
// get the text content
console.log(temp.textContent);
&#13;
&#13;
&#13;