jQuery检查目标是否为链接 [英] jQuery check if target is link

查看:91
本文介绍了jQuery检查目标是否为链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有捕获点击的全局功能。

I have a global function to capture clicks.

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

当目标是链接时,我想做其他事情,但是如果< a> 标签实际上围绕着一个div(因为HTML5允许),目标将是该div。

I want to do additional stuff when the target is a link, but if the the <a> tag actually surrounds a div (as HTML5 allows this) the target will be that div.

http://jsfiddle.net/Af37v/

推荐答案

您可以尝试查看您单击的元素是< a> 标签的子元素还是子元素。

You can try to see if the element you clicked on either is or is a child of an <a> tag.

$(document).click(function(e){
    if($(e.target).closest('a').length){
        alert('You clicked a link');
    }
    else{
        alert('You did not click a link');
    }
});

这篇关于jQuery检查目标是否为链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆