Check if event target is hyperlink

前端 未结 3 913
长情又很酷
长情又很酷 2021-02-02 06:09

I have a large div inside there which are smaller divs, achor tags and other elements. Each large div in my program is bound to \"mousedown\" event and inside the onMouseDown ha

3条回答
  •  花落未央
    2021-02-02 06:53

    You could check the tagName property, as said by @parthik-gosar. Another way is to use the instanceof operator to check the element class (hyperlinks are of type HTMLAnchorElement):

    if (event.target instanceof HTMLAnchorElement) {
      console.log(event.target.href);
    }
    

提交回复
热议问题