我正在尝试拦截所有a
链接点击以执行一些额外的工作,这适用于独立的链接,但当它包含img
时,会返回img
标记代替。
有没有办法检查上面的元素是否也是a
链接,或者我应该采用不同的方式来处理它?</ p>
JS:
document.onclick = function (e) {
e = e || window.event;
var element = e.target || e.srcElement;
if (element.tagName == 'A') {
window.open(element.href, "_system");
return false;
}
};
答案 0 :(得分:1)
您可以遍历所点击节点的父节点,以查看祖先是否为A
标记
...
var element = e.target || e.srcElement;
while (element.tagName != 'A' && element.parentNode) {
element = element.parentNode;
}
if (element.tagName == 'A') {
...
答案 1 :(得分:0)
您可以在锚标记的click事件上而不是在整个文档上附加处理程序,然后您不需要检查事件目标。
var anchorTags = document.getElementsByClassName("classname");
var myEventHandler = function() {
// stuff
};
for (var i = 0; i < anchorTags.length; i++) {
anchorTags[i].onclick = myEventHandler;
}
答案 2 :(得分:0)
我不确定您是否需要实际捕获所有点击,但如果您想要捕获实际点击的锚标记,您可以考虑以下方法。
var anchors = document.getElementsByTagName('a');
for(var i = 0, anchor; anchor = anchors[i]; i++ ) {
anchor.addEventListener('click', function (e){
e = e || window.event;
//Grabs the element that initiated the event, the anchor tag.
var element = this;
alert(element.tagName);
if (element.tagName == 'A') {
window.open(element.href, "_system");
return false;
}
})
}
答案 3 :(得分:-1)
您应该添加另一个条件来检查当前点击的元素是否是a
元素的子元素。
您可以使用 closest 方法。
Element.closest()
方法返回与参数中给出的选择器匹配的当前元素(或当前元素本身)的最近祖先。如果没有这样的祖先,它将返回null。
条件如下:
if (element.tagName == 'A' || element.closest('a') != null) {
window.open(element.closest('a').href, "_system"); //Using the parent a link `href`
return false;
}
希望这有帮助。
document.onclick = function (e) {
e = e || window.event;
var element = e.target || e.srcElement;
if (element.tagName == 'A' || element.closest('a') != null) {
alert("Open with href :"+element.closest('a').href);
return false;
}
};
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
<p>The below Google text link returns A when clicked, correct!</p>
<a href="google_link">Google Link</a>
<br />
<p>The below Bing image link returns IMG when clicked, incorrect!</p>
<a href="img_link"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAALAAAABOCAMAAAC38WwFAAAAwFBMVEX///8Adb4Acr0AcLwAbrsAd7+dxeMAb7zS5fMAe8H7qClfnc/p9Prc7Pa72e0dfsJIk8sxishqrNj0+/0Aa7qPuNxWlcvZ5PHZ6/YAabno8/pvqNXw9/u+1Op/tNtAi8ipzue11evL4PCZvd77pRtYn9GDud4Vg8Wl0Olgp9ZJmM6TweGIsdjE3O6pyORmoNF5qtYpjcn8s0T/+/X8xXj93rP/9en8v1/916T+7tj+6c38uVH7rzGIs9r90JP7ogvMNc9SAAAGjklEQVRoge2aa2OiOBSGJYRblZuoVbQoVKTKgjqd2dnZnXb+/79aMCdclICXavvB91ORJOdJcnJyEtpq3XXXXXfdddddd32I1Med7FPqvP747/fD7+//vl4LqkadwaiXaPR2Qp1fP+fzh4eH+cP7JxB32jyXCPWPr/L6PcVNNf9xPTCWzgD++4Fq/vN6YCydA/wtA/5+PTCWzgBu/QMuMf/262pcTJ0D/Prj95/5fP7n579Xw2LrHOAU+f39/YrDa2mh010uvYpXh8D2UI5iMVwsFmHsyerkVGMTVR3KqYaq3VS5o8qeKyaaRfIQtgI7cra4hyWex0/NwF785gdcD2NekiSMsWKMn93jNxXVjd/WhhkoqYLtatyPNZVVeDjrt00hYUvVQ4H/FnstVVzxPOJ2Qk3A1ss64HgExaESz+vGJipXsi1isTyAtrg2dCQh2kDyBy9x+nT8YlUY1sZTpWQrLWy2DY7Pf2kAXuglWzxfeFqVkEVh6/tbwZSLg9vlULmv1CzilO6+b7hbLm89tYSopWLNOmBHM3tQiUe6aQza67ZhKpi2yvuFFSCmLSNUAB46+cgkBDjpbuKFmXWshJ1S50bwikeKOWi3B0ZQGNlszGqAOV0BXClwYs1TJ5Zl2Z727CsYmlbEbJhEYVc0B9ZMTO1gZPrOc5zobWwIWYdRO++dNuXpMKyen2Tbsjpq5DrUUipBDw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