评估CSS选择器LTR或RTL是否更容易/更快? [英] Is it easier/faster to evaluate CSS selectors LTR or RTL?

查看:370
本文介绍了评估CSS选择器LTR或RTL是否更容易/更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个CSS选择器

ul > li a

从左到右或从右到左来评估它会更容易/更快吗? (我意识到容易和更快的答案可能不同。我想要两个答案)。我将要走下这些路径,我不想到那里,然后意识到我选择了错误的路径:)

Would it be easier/faster to evaluate it from left to right, or right to left? (I realize the answers for "easy" and "faster" might be different.. I want the answer to both). I'm about to embark down one of these paths and I don't want to get half way there and then realize I chose the wrong path :)

LTR:迭代在文档中的所有元素上,选择 ul 的那些,然后检查 li 然后查看它们的后代 a 。

LTR: Iterate over all the elements in the document, pick out the ones that are ul, then check all their children for li, and then look at their descendents for a.

RTL:迭代文档中的所有元素, a ,过滤掉没有祖先是 li 的那些 li 祖先,检查其父级是否为 ul ,如果不是,请删除 / code>。

RTL: Iterate over all the elements in the document, find all the a, filter out the ones that don't have an ancestor that is an li, if it does have an li ancestor, check if its parent is a ul, if not, drop that a.

此外,没有任何其他方法来做这件事,而不是遍历全部

Also, there isn't really any other way to do this than iterating over all the elements is there?

我想在元素的上下文中,像jQuery,

I'm thinking in the context of finding elements, as jQuery does, not in the context of applying styles.

推荐答案

浏览器和Sizzle选择器JS引擎(在jQuery和其他框架中使用的)

Browsers, and the Sizzle selector JS engine (what is used in jQuery and other frameworks) use right to left matching.

从左到右是大多数情况下最佳的遍历解决方案。

Right to left works out to be the most optimal traversing solution in most situations.

Sizzle优化以ID开头的选择器。它首先解析具有ID的元素。

Sizzle optimizes selectors that start with an ID. It resolves the element with the ID first. It then uses that as context for further traversing.

所以如果你有选择器

#myID > ul a

Sizzle会先找到#myID的元素,假设在这种情况下,是更优化的。

Sizzle will find the element with #myID first, assuming that in this case, left to right is more optimal.

这篇关于评估CSS选择器LTR或RTL是否更容易/更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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