我刚刚在查看 jQueryUI 按钮插件时注意到了这一点
$("button, input:submit, a", ".demo").button();
我从来没有见过这样的东西。这就像在一个 jQuery 选择器中进行多项选择吗?
最佳答案
第二个参数(".demo"
在您的示例中)是上下文,基本上您的选择器被限制为仅匹配确定的上下文:
$(expr, context)
就相当于使用find
方法:
$(context).find(expr)
查看 jQuery function 的文档:
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:
$('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"
称为 Multiple Selector , 在那里你可以指定任意数量的选择器来组合成一个结果,只需用逗号分隔它们。
关于jquery - jQuery 选择器的多个参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2672034/