如何检索 DOM Text 节点的文本?
对于 DOM 中给定的 Text 节点,可以使用以下属性之一来检索其文本:
textContent
data
nodeValue
wholeText< /code>
但该使用哪一个呢?哪一个最可靠且支持跨浏览器?
(如果多个属性 100% 可靠且跨浏览器,那么哪一个最合适?)
For a given Text node in the DOM, one can use one of these properties to retrieve its text:
textContent
data
nodeValue
wholeText
But which one to use? Which one is the most reliable and cross-browser supported?
(If multiple properties are 100% reliable and cross-browser, then which one would be most appropriate?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
nodeValue
应该是跨浏览器兼容的。 它是原始 DOM Level 2 规范的一部分。另请查看quirksmode.org 兼容性表就此而言(我总是用它来查看哪些浏览器支持哪些属性)。
nodeValue
should beis cross-browser compatible. It is part of the original DOM Level 2 specification.Also have a look at the compatibility table of quirksmode.org for that matter (that's what I always use to see which properties are supported by which browsers).
当您 100% 确定它是文本节点时,您可以使用其中任何一个。
When you're 100% sure that it's a text node you can use any of them.