使用onclick事件将div中的'a'的onclick事件解除绑定 [英] unbind onclick event for 'a' inside a div with onclick event

查看:417
本文介绍了使用onclick事件将div中的'a'的onclick事件解除绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

html:

<div id="container">
<div class="desc">desc</div>
<a href="foo.php">foo</a>
</div>

js:

$('#container').click(function(){
    ...
    my_function();
    ...
});

这在用户单击容器内除a标签之外的容器时有效.

This works when the user click inside container except for the a tag.

如果用户单击标签,则会触发click事件.但是,我想禁用标签的自定义点击功能.也就是说,链接到没有onlick事件的页面.

If the user click a tag, the click event fires. However, I want to disable self-defined click function for a tag. That is, link to the page without the onlick event.

这不起作用:

$('#container :not(a)').click();

推荐答案

检查事件是否源自a标记,就像这样

Check if the event originates from the a tag, like so

$('#container').click(function(e){
    if ($(e.target).is('a')) return;
    ...
    my_function();
    ...
});

这篇关于使用onclick事件将div中的'a'的onclick事件解除绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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