admin管理员组

文章数量:1022784

$(document).on('click', '.SELECTOR1 OR #SELECTOR2', function(){
        // some code
});

What I want to achieve is to run some code if either one of the elements is clicked.

$(document).on('click', '.SELECTOR1 OR #SELECTOR2', function(){
        // some code
});

What I want to achieve is to run some code if either one of the elements is clicked.

Share Improve this question asked Apr 15, 2015 at 15:40 StevikStevik 1,1522 gold badges17 silver badges38 bronze badges 1
  • write like this '.SELECTOR1, #SELECTOR2' use a – Nishit Maheta Commented Apr 15, 2015 at 15:41
Add a ment  | 

2 Answers 2

Reset to default 6

The Sizzle selector engine that jQuery uses follows the same rules as CSS. With that in mind you can separate selectors using ,:

$(document).on('click', '.SELECTOR1, #SELECTOR2', function(){
    // some code
});

Simply use a ma to do that:

$(document).on('click', '.SELECTOR1, #SELECTOR2', function(){
        // some code
});

JSFIDDLE.

$(document).on('click', '.SELECTOR1 OR #SELECTOR2', function(){
        // some code
});

What I want to achieve is to run some code if either one of the elements is clicked.

$(document).on('click', '.SELECTOR1 OR #SELECTOR2', function(){
        // some code
});

What I want to achieve is to run some code if either one of the elements is clicked.

Share Improve this question asked Apr 15, 2015 at 15:40 StevikStevik 1,1522 gold badges17 silver badges38 bronze badges 1
  • write like this '.SELECTOR1, #SELECTOR2' use a – Nishit Maheta Commented Apr 15, 2015 at 15:41
Add a ment  | 

2 Answers 2

Reset to default 6

The Sizzle selector engine that jQuery uses follows the same rules as CSS. With that in mind you can separate selectors using ,:

$(document).on('click', '.SELECTOR1, #SELECTOR2', function(){
    // some code
});

Simply use a ma to do that:

$(document).on('click', '.SELECTOR1, #SELECTOR2', function(){
        // some code
});

JSFIDDLE.

本文标签: jqueryJavascriptMultiple selectorStack Overflow