开发者

Syntax for Multiple Selectors in jQuery when using OR

I'd like to execute some javascript if button 1 is pressed OR button 2 is pressed.

I'm using the notation pasted below, (The code below doe开发者_开发技巧s not work but I've supplied it to illustrate what I am trying to do). I know I can wrap up the code to execute into another function, and then write two blocks of code (1 for each button) but I'd just like to learn if there's a way to do a conditional OR selector in jQuery using the type of sytnax below. Thanks!

$(".btn1.btn2").click(function(){
  //execute code
}


Use a comma for multiple selections in one call. The example below selects all elements that have class btn1 and/or btn2

$(".btn1, .btn2").click(function(){
  //execute code
}


$("#btn1, #btn2").click(function(){
  //execute code
}

if you have id's for the button.

$(".btn1, .btn2").click(function(){
  //execute code
}

if you want to access them using class name.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜