jQuery check if target is link

后端 未结 7 1394
傲寒
傲寒 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:14

    If the exact target is link, then you can use .is()

    Example:

    $(".element").on("click", function(e){
      if($(e.target).is("a")){
        //do your stuff
      }
    });
    

    EDIT:

    If it is surrounded by other element that is inside an anchor tag, then you can use closest() and check whether it have anchor tag parent or not by using length

    Example:

    $(".element").on("click", function(e){
      if($(e.target).closest("a").length){
        //do your stuff
      }
    });
    

提交回复
热议问题