Jquery:在一个.click事件中使用两个选择器?

Wra*_*Lux 8 jquery jquery-selectors

如果我有这样的事情:

$(".jq_elements").children("input").click(function()
Run Code Online (Sandbox Code Playgroud)

如何向其添加另一个选择器,以便在单击任何一个时触发相同的事件.

基本上需要这样的东西:

$(".jq_elements, .jq_another_div").children("input").click(function()
Run Code Online (Sandbox Code Playgroud)

这样的事情可能吗?

编辑:我按照上面描述的方式尝试了这个.事实证明我的代码有点歪,但现在很好.

Ray*_*nos 17

$("selector, selector")

这种语法很有效.你想要同时打电话.children("input")吗?

另外 $.merge($("selector"), $("selector"));


Jar*_*ish 5

你展示的方式应该有效.

http://api.jquery.com/multiple-selector/

你需要的只是一个逗号分隔符.

编辑:你为什么打电话给孩子()?

$(".jq_elements > input, jq_another_div > input").click(function(){
    // ?
});
Run Code Online (Sandbox Code Playgroud)

空间是一个选择器,意味着所有后代都匹配以下选择.因此,.jq_another_div input将使用jq_another_div作为类名在元素中查找所有子输入.

http://api.jquery.com/child-selector/

http://api.jquery.com/descendant-selector/