jQuery选择器的多个参数? [英] Multiple Parameters for jQuery selector?
问题描述
我只是看着jQueryUI按钮插件,并注意到了这一点
I was just looking at the jQueryUI button plug-in and noticed this
$("button, input:submit, a", ".demo").button();
我从未见过这样的事情.这就像在一个jQuery选择器中进行多次选择吗?
I never seen something like this. Is this like multiple selects in one jQuery selector?
推荐答案
第二个参数(在您的示例中为".demo"
)是 context ,基本上,您的选择器被限制为仅匹配a的后代.确定的上下文:
The second argument (".demo"
in your example) is the context, basically your selector is restricted to match only descendants of a determined context:
$(expr, context)
等同于使用 find
方法:
Is just equivalent to use the find
method:
$(context).find(expr)
请查看 jQuery函数的文档:
选择器上下文
Selector Context
默认情况下,选择器执行其
从DOM开始的DOM搜索
文档根目录.但是,
可以为
通过使用可选的第二个进行搜索
$()
函数的参数.为了
例如,如果在回调函数中
我们希望搜索一个元素,
我们可以限制搜索范围:
By default, selectors perform their
searches within the DOM starting at
the document root. However, an
alternate context can be given for the
search by using the optional second
parameter to the $()
function. For
example, if within a callback function
we wish to do a search for an element,
we can restrict that search:
$('div.foo').click(function() {
$('span', this).addClass('bar');
// it will find span elements that are
// descendants of the clicked element (this)
});
还要注意,您发布的"button, input:submit, a"
选择器称为多个选择器,并且在那里,您可以指定任意数量的选择器以组合成一个结果,只需用逗号将它们分开即可.
Also notice that the selector you post "button, input:submit, a"
, is called Multiple Selector, and there you can specify any number of selectors to combine into a single result, just by separating them by a comma.
这篇关于jQuery选择器的多个参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!