innerHTML without the html, just text [duplicate]

一个人想着一个人 提交于 2019-12-17 15:48:16

问题


I've created an e-mail link that automatically populates the necessary information in the body. But, when I do .innerHTML I get a little more than I bargained for.

I want "March, 2012: 12-16"

What I get <B>March, 2012</B>: <FONT color=blue>12</FONT> - <FONT color=blue>16</FONT>

Is there a way to get the innerHTML without the html tags?

.value = undefined
.text = undefined

回答1:


You want .textContent in all but older IE, and .innerText in IE (<9).

So, try:

string = (node.textContent===undefined) ? node.innerText : node.textContent;

EDIT: Or, just use GGG's much cleaner string = (node.innerText || node.textContent), since undefined is falsy.




回答2:


In the browser that supports the standard, you can use textContent instead of innerHTML. Otherwise you can loop through the next nodes and concatenate them, or using library like jQuery that abstract this approach for you.




回答3:


If you are already using jQuery, you can use .text():

http://jsfiddle.net/vkgYR/

if you are not using it though, you should just go with the other comments since it would be silly to load all of jQuery just for the .text() method



来源:https://stackoverflow.com/questions/9727111/innerhtml-without-the-html-just-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!