微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

jquery – 在序列中排序简单的选择器

这仅仅是一个关于良好实践的概念性问题.

问题很简单.是否有任何“正确”的方式来订购jQuery / CSS选择器的部分?例如,假设我有一个具有以下属性的元素:

<div class="red green blue" id="someDiv" anAttr="something">

为了更好地衡量,我们也会在它被徘徊时瞄准它.

以下选择器之间有什么区别吗?:

div.red.green.blue#someDiv[anAttr='something']:hover
div#someDiv.red.green.blue[anAttr='something']:hover
div.red#someDiv[anAttr='something']:hover.blue.green
div[anAttr='something'].blue:hover#someDiv.red.green

意思是,目标的顺序是否重要,真的吗?或者我可以把它们放在任何 – 我想要的顺序?

根据这个JSFiddle,他们都在技术上工作.

解决方法

5.2 Selector Syntax

A simple selector is either a type selector or universal selector
followed immediately by zero or more attribute selectors,ID
selectors,or pseudo-classes,in any order. The simple selector
matches if all of its components match.

Note: the terminology used here in CSS 2.1 is different from what is
used in CSS3. For example,a “simple selector” refers to a smaller
part of a selector in CSS3 than in CSS 2.1. See the CSS3 Selectors
module [CSS3SEL].

A selector is a chain of one or more simple selectors separated by
combinators. Combinators are: white space,“>”,and “+”. White space
may appear between a combinator and the simple selectors around it.

The elements of the document tree that match a selector are called
subjects of the selector. A selector consisting of a single simple
selector matches any element satisfying its requirements. Prepending a
simple selector and combinator to a chain imposes additional matching
constraints,so the subjects of a selector are always a subset of the
elements matching the last simple selector.

One pseudo-element may be appended to the last simple selector in a
chain,in which case the style information applies to a subpart of
each subject.

来自:w3.org

原文地址:https://www.jb51.cc/jquery/177164.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐