Check which element has been clicked with jQuery

前端 未结 7 674
天命终不由人
天命终不由人 2020-12-24 07:10

I am trying to use an \'if\' statement to determine which element was clicked.

Basically I am trying to code something along the lines of:

if (the el         


        
7条回答
  •  -上瘾入骨i
    2020-12-24 07:34

    Use this, I think I can get your idea.

    Live demo: http://jsfiddle.net/oscarj24/h722g/1/

    $('body').click(function(e) {
    
        var target = $(e.target), article;
    
        if (target.is('#news_gallery li .over')) {
           article = $('#news-article .news-article');
        } else if (target.is('#work_gallery li .over')) {
           article = $('#work-article .work-article');
        } else if (target.is('#search-item li')) {
           article = $('#search-item .search-article');
        }
    
        if (article) {
           // Do Something
        }
    });​
    

提交回复
热议问题