javascript jQuery 检查目标是否为链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14327489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery check if target is link
提问by XCS
I have a global function to capture clicks.
我有一个全局函数来捕获点击。
$(document).click(function(e){
//do something
if(clickedOnLink)
//do something
});
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.
当目标是一个链接时,我想做一些额外的事情,但如果<a>
标签实际上围绕着一个 div(因为 HTML5 允许这样做),目标将是那个 div。
回答by Rocket Hazmat
You can try to see if the element you clicked on either is or is a child of an <a>
tag.
您可以尝试查看您单击的元素是否是某个<a>
标签的子元素。
$(document).click(function(e){
if($(e.target).closest('a').length){
alert('You clicked a link');
}
else{
alert('You did not click a link');
}
});
回答by James Montagne
I believe using is
will actually have better performance than the answers suggesting closest
:
我相信使用is
实际上会比建议的答案具有更好的性能closest
:
$(e.target).is('a, a *');
This checks if the element itself is an a
or if it is contained with an a
.
这将检查元素本身是否是 ana
或者它是否包含在a
.
This should be faster than closest
because it will use matcheson the element itself and not need to traverse up the DOM tree as closest
will do.
这应该比closest
因为它将在元素本身上使用匹配并且不需要像往常那样遍历 DOM 树而更快closest
。
回答by kreig
Try this
试试这个
$(document).click(function(e){
//do something
if($(this).closest('a').length)
//do something
});
回答by Ari
If the exact target is link, then you can use .is()
如果确切的目标是链接,那么您可以使用 .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
如果它被锚标记内的其他元素包围,那么您可以使用closest()
并检查它是否具有锚标记父级length
Example:
例子:
$(".element").on("click", function(e){
if($(e.target).closest("a").length){
//do your stuff
}
});
回答by Greg
With jquery just get the tagName attribute $("a").prop("tagName");
使用 jquery 只需获取 tagName 属性 $("a").prop("tagName");
回答by Robin Drexler
Updated: You could check if the target is an a or if a parent is an a.
更新:您可以检查目标是 a 还是父级是 a。
$(function () {
$(document).on('click', function (e) {
$target = $(e.target);
if ($target.closest('a').length > 0) {
alert('i am an a');
}
});
});
回答by SeinopSys
You can test if there's a <div>
under <a>
by testing if the .children()
<div>
has anything inside it. If nothing is inside, or there is no <div>
, the if
statement will return false
.
您可以通过测试里面是否有任何东西来测试是否有一个<div>
under 。如果里面什么都没有,或者没有,语句将返回。<a>
.children()
<div>
<div>
if
false
I suggest this code:
我建议使用以下代码:
$(document).click(function(e){
var willRedirect = ($('a[href="/"]').attr('href').indexOf('#') == -1 ? true : false),
//run code
if ( willRedirect === false ){
e.preventDefault();
//the link will not redirect
if ( $(this).children('div').html() ){
//there is a <div> inside <a> containing something
}
else {
//there is no <div> inside <a>
}
}
else {
//the link is not pointing to your site
}
});