Html 检查事件目标是否为超链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15661343/
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
Check if event target is hyperlink
提问by user1240679
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 handler, I basically check the event.target.
我里面有一个大 div,里面有较小的 div、achor 标签和其他元素。我程序中的每个大 div 都绑定到“mousedown”事件,并且在 onMouseDown 处理程序中,我基本上检查了 event.target。
If a user clicks on an items that is a hyper link, I want to check if event.target was hyperlink and then navigate to that link if event.target was a hyperlink. How can that be done?
如果用户单击作为超链接的项目,我想检查 event.target 是否是超链接,然后如果 event.target 是超链接,则导航到该链接。那怎么办呢?
Here's the structure of the divsa and elements.
这是 divsa 和元素的结构。
<div class="camp-name">
<span class="btnCampaign"><div class=""></div></span>
<span class="campaign-name">
<a href="http://www.google.com">Some Link here</a>
</span>
</div>
<div class="campaign-name-sub">
<span class="campaign-accountname">Some Text here</span>
<span class="seprator">|</span>
<span class="brandname">Some Text here</span>
</div>
JS
JS
var label = label.createElement("DIV");
label.innerHMTL = src //src is the above html that is seen here
Plugin.addEventListener(label, "mousedown", params.onMouseDown);
Plugin.onMouseDown() = function(event) {
var target = (event.currentTarget) ? event.currentTarget : event.srcElement;
if (target.tagName.toLowerCase() === "a" && target !== undefined) {
console.log(target.href);
Plugin.stopPropagation(event);
}
};
回答by Parthik Gosar
You should get it through
你应该通过
if(event.target.tagName.toLowerCase() === 'a')
{
event.target.href; //this is the url where the anchor tag points to.
}
回答by CedX
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
):
tagName
正如@parthik-gosar 所说,您可以检查该物业。另一种方法是使用instanceof
运算符检查元素类(超链接的类型为HTMLAnchorElement
):
if (event.target instanceof HTMLAnchorElement) {
console.log(event.target.href);
}
回答by theshadowmonkey
I tried modifying your code and there were multiple problems with the code and corrected them.
我尝试修改您的代码,但代码存在多个问题并进行了更正。
This is the JavaScript part
这是 JavaScript 部分
var src = '<div class="camp-name"><span class="btnCampaign"><div class=""></div></span><span class="campaign-name"><a href="http://www.google.com">Some Link here</a></span></div>';
var label = document.createElement('DIV')
label.innerHTML = src;
var topdiv = document.getElementById("test");
console.log(label)
topdiv.appendChild(label);
label.addEventListener('click', test, false);
function test(event) {
if(event.target.tagName.toLowerCase() === 'a') {
var href = event.target.href;
console.log(href);
}
window.location = href;
};
This is the HTML part of it
这是它的 HTML 部分
<div id="test">
test
</div>
I did this in a jsfiddle http://jsfiddle.net/KDejm/1/
我在 jsfiddle http://jsfiddle.net/KDejm/1/ 中做了这个
Please let me know if the vent is captured correctly.
请让我知道通风口是否正确捕获。