如何检索DOM文本节点的文本? [英] How to retrieve the text of a DOM Text node?

查看:81
本文介绍了如何检索DOM文本节点的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于DOM中的给定Text节点,可以使用这些属性之一来检索其文本:




  • textContent
  • 数据

  • nodeValue

  • 全文



    • 但是要使用哪一个? =哪一个是最可靠和跨浏览器...



      (如果多个属性是100%可靠和跨浏览器,那么哪一个最适合? )






      更新:



      实际上, wholeText 不是一个选项,因为它返回文本节点和所有兄弟文本节点的连接文本...

      解决方案

      nodeValue 应该是跨浏览器兼容的。 它是原始DOM 2级规范的一部分。



      还可以看看 quirksmode.org的兼容性表(这是我经常用来查看哪些浏览器支持哪些属性)。


      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...

      (If multiple properties are 100% reliable and cross-browser, then which one would be most appropriate?)


      Update:

      Actually, wholeText is not an option since it returns the concatenated text of the text node and all sibling text nodes...

      解决方案

      nodeValue should be is 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).

      这篇关于如何检索DOM文本节点的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆