检查事件目标是否为超链接 [英] Check if event target is hyperlink

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

问题描述

我里面有一个大div,其中有较小的div,achor标签和其他元素. 我程序中的每个大div都绑定到"mousedown"事件,在onMouseDown处理程序内部,我基本上检查event.target.

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.

如果用户单击作为超链接的项目,我想检查event.target是否为超链接,如果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?

这是divsa和元素的结构.

Here's the structure of the divsa and elements.

<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

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);
        }
};

推荐答案

您应该通过

if(event.target.tagName.toLowerCase() === 'a')
{
    event.target.href; //this is the url where the anchor tag points to.
}

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

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