jQuery 选择器的多个参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2672034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:00:17  来源:igfitidea点击:

Multiple Parameters for jQuery selector?

jquery

提问by chobo2

I was just looking at the jQueryUI button plug-in and noticed this

我只是在看 jQueryUI 按钮插件并注意到这一点

$("button, input:submit, a", ".demo").button();

I never seen something like this. Is this like multiple selects in one jQuery selector?

我从来没有见过这样的事情。这就像一个 jQuery 选择器中的多个选择吗?

回答by CMS

The second argument (".demo"in your example) is the context, basically your selector is restricted to match only descendants of a determined context:

第二个参数(".demo"在您的示例中)是context,基本上您的选择器仅限于匹配确定的context 的后代:

$(expr, context)

Is just equivalent to use the findmethod:

正好相当于使用find方法:

$(context).find(expr)

Give a look to the documentation of the jQuery function:

查看jQuery 函数的文档:

Selector Context

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:

选择器上下文

默认情况下,选择器在 DOM 中从文档根开始执行它们的搜索。但是,可以通过使用$()函数的可选第二个参数为搜索提供替代上下文。例如,如果在回调函数中我们希望搜索一个元素,我们可以限制该搜索:

$('div.foo').click(function() {
  $('span', this).addClass('bar');
  // it will find span elements that are
  // descendants of the clicked element (this)
});

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.

另请注意,您发布的选择器"button, input:submit, a"称为Multiple Selector,您可以在其中指定任意数量的选择器以组合成单个结果,只需用逗号分隔它们即可。