简体   繁体   English

CSS DOM遍历是否优于JavaScript DOM遍历?

[英]CSS DOM Traversal superior than JavaScript DOM Traversal?

JavaScript and CSS both use its own DOM tree when traversing through HTML elements. 在遍历HTML元素时, JavaScriptCSS都使用自己的DOM树。

In JavaScript , we can use its own DOM traversal methods such as JavaScript中 ,我们可以使用自己的DOM遍历方法,例如

element.parentNode
element.nextSibling

However, this way is very unintuitive because JavaScript 's DOM Tree contains things other than the HTML elements which can be easy to mess up for a developer. 但是,这种方式非常不直观,因为JavaScript的DOM树包含HTML元素以外的内容,这些内容对于开发人员来说很容易搞乱。

ie

<div>
     <p>
     </p>
</div>

JavaScript 's DOM Tree has <div> as the parent and 3 children: JavaScript的DOM树具有<div>作为父级和3个子级:

text node: \\n\\t 文字节点: \\n\\t

element node: <p> 元素节点: <p>

text node: \\n 文字节点: \\n

This becomes increasingly more difficult to keep track as the HTML document structure increases in complexity because not all HTML elements have a newline before and after among other things. 随着HTML文档结构的复杂性增加,跟踪变得越来越困难,因为并非所有HTML元素在前后都有换行符。

Thankfully, JavaScript allows us to use CSS 's DOM traversal using: 幸运的是, JavaScript允许我们通过以下方式使用CSS的DOM遍历:

element.querySelector("CSSselectorHere")

Since CSS 's DOM Tree only contains the HTML elements, that makes it much easier to implement. 由于CSS的DOM树仅包含HTML元素,因此易于实现。

The case only I could think of where JavaScript 's DOM Tree would be advantageous is if you are trying to color the text "cdf" red in the following: 只有我想到JavaScript的DOM树在哪里会比较有优势,这是如果您尝试将文本“ cdf”涂成红色,如下所示:

In HTML: 在HTML中:

<p> abc <a href="...">link</a> cdf </p>

In JavaScript : JavaScript中

pElementVariable.firstChild.nextSibling.nextSibling.classList.add("CSSclassRed");

However, wouldn't a better HTML practice be to just enclose the unique text like so? 但是,更好的HTML做法不是像这样封装唯一的文本吗?

<p> abc <a href="...">link</a> <span>cdf</span> </p>

Thus, styling the span using CSS 's DOM traversal is possible. 因此,可以使用CSS的DOM遍历对跨度进行样式设置 (assuming we're using traversal methods only, no ids) (假设我们仅使用遍历方法,没有id)

If so, does .querySelector , an enabler in JavaScript for CSS 's DOM traversal, make JavaScript 's own built-in DOM traversal methods obsolete? 如果是这样, .querySelectorJavaScript中用于CSS的DOM遍历的启动器)是否会使JavaScript自己的内置DOM遍历方法过时?

No. CSS is more limited, because it can only select elements (and pseudo-elements, but not through querySelector , but that might change). 不行。CSS受更多限制,因为它只能选择元素(和伪元素,但不能通过querySelector ,但可能会改变)。

Using the DOM you can traverse arbitrary nodes in the tree. 使用DOM,您可以遍历树中的任意节点。 That's more powerful. 那更强大。 And if you want to restrict to elements, you can: 如果您想限制元素,则可以:

node.childNodes; // All child nodes
parentNode.children; // Only element child nodes

node.firstChild; // First node child
parentNode.firstElementChild; // First element child

node.lastChild; // Last node child
parentNode.lastElementChild; // Last element child

node.previousSibling; // The previous sibling node
nonDoctypeChildNode.previousElementSibling; // The previous sibling element

node.nextSibling; // The next sibling node
nonDoctypeChildNode.nextElementSibling; // The next sibling element

node.parentNode; // The parent node
node.parentElement; // The parent node, only if it's an element

So you don't need CSS APIs for things like that. 因此,您不需要CSS API这样的东西。 And in particular you shouldn't modify your HTML to be able to use CSS APIs. 特别是,您不应修改HTML以使用CSS API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM