最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Javascript - Multiple selector - Stack Overflow

programmeradmin1浏览0评论
$(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.

发布评论

评论列表(0)

  1. 暂无评论