Jquery: using two selectors for one .click event?

心不动则不痛 提交于 2019-12-23 07:49:59

问题


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.


回答1:


$("selector, selector")

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

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




回答2:


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/



来源:https://stackoverflow.com/questions/4879888/jquery-using-two-selectors-for-one-click-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!