简体   繁体   English

单击listerner for div但不链接

[英]click listerner for div but not link

I have a click listener on a div and inside the div I have a link with a normal -tag but whenever I click on the link the event is also run. 我在div上有一个点击监听器,在div里面我有一个带有普通-tag的链接,但每当我点击链接时,事件也会运行。 How can I prevent the stuff within the click-listener to run if the click on the link inside the div? 如果单击div中的链接,如何防止click-listener中的内容运行?

HTML: HTML:

<div class="container item-container" data="skarp-version-av-os-x-10.8.3-slappt">
        <div class="item">
            <div class="number">
                <p class="number-data">1</p>
            </div>
            <div class="upvotes btn tooltip-points" points="4" id="90" data-toggle="tooltip" title="" data-original-title="Visar poäng satta de senaste 24 h">
                4 poäng
            </div>
            <div class="item-content">
                <a href="http://feeds.idg.se/~r/idg/ETkj/~3/XSuYKgEiumE/skarp-version-av-os-x-1083-slappt" target="_blank" class="item-link fill-div">Skarp version av OS X 10.8.3 släppt</a>
                <span class="item-source">(feeds.idg.se)</span>
                <p class="item-author">Upplagd av Anonym - <em>2013-03-17 21:07:08</em></p>
            </div>
            <div class="comment">
                <i class="icon-comment"></i> <a href="http://www.tldr.nu/item/view/skarp-version-av-os-x-10.8.3-slappt#disqus_thread">0 Comments and 0 Reactions</a>
            </div>
        </div>
    </div>

Javascript: 使用Javascript:

$('.item-container').click(function(event) {
    var url = $(this).attr('data') ? $(this).attr('data') : '';
    if(url != '')
        window.location = getBaseURL() + 'item/view/' + url;
});

You could either add an event handler on the link that stops the click event from bubbling up to the div, or you could add an if-statement inside the handler for the div so it ignores the click event when the event target is the link. 您可以在链接上添加一个事件处理程序来阻止click事件冒泡到div,或者您可以在div的处理程序中添加if语句,以便在事件目标是链接时忽略click事件。 I think the former solution would be the way to go. 我认为以前的解决方案是可行的方法。

This sets an event handler on the link that stops the click event from propagating up to the div: 这会在链接上设置一个事件处理程序,阻止click事件传播到div:

$('.item-container a').click(function(event) {
   event.stopPropagation();
});

jsfiddle demo jsfiddle演示

You'll either need to bind a specific prevention method to the inner elements, like this - 您需要将特定的预防方法绑定到内部元素,如下所示 -

$("a").click(function(e){
    e.stopPropagation();
});

or you'll need to sniff for the clicked element's type - 或者你需要嗅探点击元素的类型 -

$(".wrap").click(function(e){
    if ($(e.target).hasClass("wrap")){
       alert("click");
    }
});

The second method is usually preferable, since for this question to arise, your inner elements have events of their own that you want to function fairly normally. 第二种方法通常是可取的,因为出现这个问题,你的内部元素有自己想要正常运作的事件。

However, maybe you want the links to not trigger the parent div effect when they're clicked, in which case the first is better. 但是,也许您希望链接在单击时不触发父div效果,在这种情况下,第一个更好。

Fiddle - http://jsfiddle.net/WXzjP/ 小提琴 - http://jsfiddle.net/WXzjP/

$('.item-container').click(function(event) {
    if(!$(event.target).is('a')) {
        var url = $(this).attr('data') ? $(this).attr('data') : '';
        if(url != '')
            window.location = getBaseURL() + 'item/view/' + url;
    }
});

Check if the event target that initiated the event is an anchor element, and if it isn't, execute your function. 检查发起事件的事件目标是否为锚元素,如果不是,则执行您的函数。

Familiarize yourself with Event Bubbling - you'll need to understand the basics to eventually understand Event Delegation . 熟悉事件冒泡 - 您需要了解最终了解事件委派的基础知识。

You can solve your problem in one of two ways. 您可以通过以下两种方式之一解决问题。 Either bind a click event to the <a> within your <div> and call event.stopPropagation(); 将click事件绑定到<div><a>并调用event.stopPropagation(); , or have the <div> 's event handler check the event.target for being an <a> element: ,或让<div>的事件处理程序检查event.target是否为<a>元素:

if (event.target.nodeName.toLowerCase() === 'a') {
  // <a> was clicked
}

Because it "bubbles up". 因为它“冒泡”。 Think of it like this; 想想这样;

When you click on your link, it has an action to do as you specify: navigate to a url.. Next, it "bubbles-up" the tree and you also have a click handler for the div, so it tries to run that too. 当你点击你的链接时,它有一个你指定的动作:导航到一个URL ..接下来,它“冒泡”树,你也有一个div的点击处理程序,所以它试图运行太。

If you also had a click handler for your $('body') that would also be called. 如果你还有一个你的$('body')的点击处理程序也会被调用。

You have to stop bubbling with event.stopPropagation(); 你必须停止冒泡event.stopPropagation();

eg 例如

$('#your-anchor-tag').click(function(e) {
 e.stopPropagation();
}

That way, you will stop the bubbling up process.. 这样,你将停止冒泡过程..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM