Jquery: using two selectors for one .click event?

2019-06-16 12:19发布

If I have something like this:

$(".jq_elements").children("input").click(function()

How do I add another selector to it, so that same event gets fired up when either one is clicked.

Basically something like this would be needed:

$(".jq_elements, .jq_another_div").children("input").click(function()

Is something like this possible?

edit: I did try this the way that I described above. Turns out my code was a bit crooked, but its fine now.

2条回答
Deceive 欺骗
2楼-- · 2019-06-16 12:35

$("selector, selector")

That very syntax works. Do you want to call .children("input") on both?

Alternatively $.merge($("selector"), $("selector"));

查看更多
Fickle 薄情
3楼-- · 2019-06-16 12:55

The way you showed should work.

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

All you need is a comma delimiter.

EDIT: Why are you calling children()?

$(".jq_elements > input, jq_another_div > input").click(function(){
    // ?
});

The space is a selector, meaning all descendents that match the following select. So .jq_another_div input will find all child inputs within the element with jq_another_div as a classname.

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

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

查看更多
登录 后发表回答