选择器中的运算符优先级
Operator precedence in selectors
当我写的时候:
$(tableElement).find("thead input,img")
jQuery 是否会匹配 theads 以及 images 中的输入?
或者它会匹配广告中的输入以及广告中的图像吗?
更一般地说,jQuery 选择器中的运算符优先级是什么?我到处都找不到它。
更新:
鉴于优先级,我可以使用括号或其他方式在选择器字符串中更改它吗?
当我写优先级时,我是在谈论选择器字符串运算符被 jQuery 解析的优先级,而不是关于哪些元素首先包含在结果 jQuery 集合中。
选择器一目了然。您要求它查找:
tableElement // The table element's
thead input // inputs under thead of table element and
img // images under the table element
这就像:
.tableElement thead input,
.tableElement img {
}
它选择 table 元素下的图像并在 table 元素下的 thead 下输入。我不确定这里的优先级是什么。
如果你想在同一个父级下使用类似的东西,你需要使用:
$(tableElement).find("thead").find("input, img")
$(tableElement).find("thead input, thead img")
以上将翻译为:
.tableElement thead input,
.tableElement thead img {
}
此代码将同时获得 thead
内的 img
和 input
出现在 tableElement
.
下
When I wrote precedence, I was talking about the precedence at which the selector string operators are parsed by jQuery, not anything about which elements are included first in the resulting jQuery collection.
好的,回答你最近的问题,没有特定的顺序。它是完全无序的并选择所有内容。 jQuery 紧跟在 CSS 选择器之后。它没有任何特定的解析顺序。在内部,一切都是 querySelectorAll()
和循环。
通常 - jQuery 中的逗号是分隔符。
字符串的每个部分都单独计算,然后组合,所以在你的例子中 - 你正在寻找 thead input
AND img
里面 $(tableElement)
.
至于优先级 - 他们将 return 在 DOM
内的位置。
当我写的时候:
$(tableElement).find("thead input,img")
jQuery 是否会匹配 theads 以及 images 中的输入?
或者它会匹配广告中的输入以及广告中的图像吗?
更一般地说,jQuery 选择器中的运算符优先级是什么?我到处都找不到它。
更新:
鉴于优先级,我可以使用括号或其他方式在选择器字符串中更改它吗?
当我写优先级时,我是在谈论选择器字符串运算符被 jQuery 解析的优先级,而不是关于哪些元素首先包含在结果 jQuery 集合中。
选择器一目了然。您要求它查找:
tableElement // The table element's
thead input // inputs under thead of table element and
img // images under the table element
这就像:
.tableElement thead input,
.tableElement img {
}
它选择 table 元素下的图像并在 table 元素下的 thead 下输入。我不确定这里的优先级是什么。
如果你想在同一个父级下使用类似的东西,你需要使用:
$(tableElement).find("thead").find("input, img")
$(tableElement).find("thead input, thead img")
以上将翻译为:
.tableElement thead input,
.tableElement thead img {
}
此代码将同时获得 thead
内的 img
和 input
出现在 tableElement
.
When I wrote precedence, I was talking about the precedence at which the selector string operators are parsed by jQuery, not anything about which elements are included first in the resulting jQuery collection.
好的,回答你最近的问题,没有特定的顺序。它是完全无序的并选择所有内容。 jQuery 紧跟在 CSS 选择器之后。它没有任何特定的解析顺序。在内部,一切都是 querySelectorAll()
和循环。
通常 - jQuery 中的逗号是分隔符。
字符串的每个部分都单独计算,然后组合,所以在你的例子中 - 你正在寻找 thead input
AND img
里面 $(tableElement)
.
至于优先级 - 他们将 return 在 DOM
内的位置。