点击链接不应触发父母onclick-event [英] click on link should not trigger parental onclick-event

查看:68
本文介绍了点击链接不应触发父母onclick-event的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

<div onclick="alert('div event');" style="cursor:pointer">
    some text
    <a href="asd.php" id="link">click</a>
</div>

当有人单击链接时,将触发javaschipt事件.我希望该事件仅在有人单击文本或div容器内的空白区域时触发;而不是有人点击链接.

When somebody clicks on the link the javaschipt event is triggered. I want that the event is only triggers if somebody clicks on the text or on the empty space inside the div container; and not if somebody clicks on the link.

事件触发时是否可以调用函数,该函数检查用户单击了哪些元素.链接

Is it possible to call a function when the event is triggered, which checks on which elements the user has clicked. something link

onclick="foo(caller);"

function foo(element){
    if(element!='link'){
        alert('yes');   
    }
} 

推荐答案

在您的链接中添加一个点击处理程序,并停止向父div冒泡的事件.像这样:

Add a click handler to your link and stop event bubbleing to the parent div. Like this:

$('#link').click(function(e) {
  e.stopPropagation();
});

这篇关于点击链接不应触发父母onclick-event的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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