问题
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