【问题标题】:What is considered a text node?什么是文本节点?
【发布时间】:2018-11-18 22:37:57
【问题描述】:

一般如果HTML中的标签之间有文本,它就变成了一个文本节点。然而,这并非总是如此。考虑这个例子:

<p>
  <a href="hello.html">hello</a> <a href="world.html">world</a>
</p>

这里,&lt;p&gt; 和第一个 &lt;a ...&gt; 之间的换行符和 2 个空格不会在 DOM 中显示为文本节点,至少在 Chrome 中不会。 helloworld 当然是文本节点。似乎只有空格的序列不会显示为文本节点,但情况并非总是如此:在此示例中,两个链接之间的空间是一个文本节点。

Chrome 如何决定什么是文本节点?

【问题讨论】:

  • Chrome 无法决定。它在 DOM 规范中清楚地 - 就规范而言 - 指出了这是如何确定的。

标签: html google-chrome dom textnode


【解决方案1】:

在 Chrome 中试试这个,你会看到 firstChildpText

let p = document.querySelector('p')

console.log(p.firstChild.constructor.name)
<p>
  <a href="hello.html">hello</a> <a href="world.html">world</a>
</p>

编辑:

看起来你甚至无法在 Chrome 中尝试,因为它会抛出 "Uncaught SecurityError: Failed to read the 'localStorage' property from 'Window': The document is sandboxed and lacks the 'allow-same-origin' flag."

SO 需要更新他们的 sn-p 实现,因为它显然已经被破坏了。

编辑 2:

您可以在 Chrome 中查看:https://codepen.io/anon/pen/qQPdbz?editors=1111

【讨论】:

  • 哎呀!我确信如果它没有显示在元素视图中,那么它就不存在......
猜你喜欢
  • 1970-01-01
  • 2021-04-30
  • 2013-06-16
  • 2010-12-02
  • 1970-01-01
  • 2017-02-05
  • 2016-12-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多