jQuery check if target is link

后端 未结 7 1526
傲寒
傲寒 2021-02-07 23:49

I have a global function to capture clicks.

$(document).click(function(e){
  //do something
  if(clickedOnLink)
    //do something
});

I want t

7条回答
  •  自闭症患者
    2021-02-08 00:26

    I believe using is will actually have better performance than the answers suggesting closest:

    $(e.target).is('a, a *');
    

    This checks if the element itself is an a or if it is contained with an a.

    This should be faster than closest because it will use matches on the element itself and not need to traverse up the DOM tree as closest will do.

提交回复
热议问题