开发者

Using more then 1 item for a .click()

Is this possible?

Code:

$('#goRightBtn').click(function() {
        Random code here
};

I have a button, when you click that button (goRightBtn) it will do all of the functions and whatnot inside that code up there. But I also have another button that will do the exact same thing (don't ask why.. Just need it!) How do I make it so that I can somehow make that code work when either one of the btns are selected..

Would something like

$('#goRightBtn','#secondBtn').click(function() {
        ///Random code he开发者_Python百科re
};

work?

Please help! :)


$('#goRightBtn, #secondBtn').click(function() {}

If you specify a second argument, that means you are specifying the context. Otherwise just put the ids in your initial argument's string like above.

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


The following should work (a comma wrapping them)

$('#goRightBtn, #secondBtn').click(function() {
        ///Random code here
};

Or you can use the add method also:

$('#goRightBtn').add('#secondBtn').click(function() {
        ///Random code here
};


Almost had it right :) It's like this (called a multiple selector):

$('#goRightBtn, #secondBtn').click(function() {
  ///Random code here
});


working demo

$('#goRightBtn, #secondBtn').click(function() { 
        alert('test');
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜