开发者

Syntax for Multiple Selectors in jQuery when using OR

开发者 https://www.devze.com 2022-12-14 01:35 出处:网络
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 illustr

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

精彩评论

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