jquery - jQuery 选择器的多个参数?

标签 jquery

我刚刚在查看 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/

相关文章:

javascript - 在 jQuery 中选择多种元素类型

javascript - 如何将 "undo"文本以编程方式插入文本区域?

javascript - 在 # 之后查找 url 中的姓氏 - jquery/javascript

php - "xmlResult.Result is undefined"错误

javascript - javascript类中的神秘错误

jquery 删除选中表格行的背景色

javascript - 试图让日期选择器与自定义假期、思想和没有周末一起玩得很好

javascript - 将元素添加到 DOM 中的深层元素

javascript - javascript 中的 Json 响应未正确循环

javascript - 关闭 Bootstrap 模式后重置验证消息