获取innerHTML值 - 仅纯文本

时间:2015-11-21 15:27:11

标签: javascript html text

这就是我要做的......

alert(document.getElementById('test').innerHTML);
<div id="test">
  hi..my name is <div>Soul</div> and I am <strong>23 years old</strong>
  Thank you.
</div>

警告框将显示:

hi..my name is <div>Soul</div> and I am <strong>23 years old</strong>Thank you.

但我在ALERT BOX中想要的是:

hi..my name is Soul and I am 23 years old Thank you.

1 个答案:

答案 0 :(得分:3)

使用 textContent 代替 innerHTML 获取文字内容而不是标记

&#13;
&#13;
<div id="test">
  hi..my name is <div>Soul</div> and I am <strong>23 years old</strong>
  Thank you.
</div>
&#13;
{{1}}
&#13;
&#13;
&#13;