jquery停止子触发父事件 [英] jquery stop child triggering parent event

查看:42
本文介绍了jquery停止子触发父事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 div,我已将 onclick 事件附加到该 div.在这个 div 中有一个带有链接的标签.当我单击链接时,还会触发来自 div 的 onclick 事件.如何禁用此功能,以便在 div onclick 上单击链接时不会触发?

脚本:

$(document).ready(function(){$(".header").bind("点击", function(){$(this).children(".children").toggle();});})

html 代码:

<a href="link.html">一些链接</a><ul class="儿童"><li>一些列表</li>

解决方案

这样做:

$(document).ready(function(){$(".header").click(function(){$(this).children(".children").toggle();});$(".header a").click(function(e) {e.stopPropagation();});});

如果您想阅读有关 .stopPropagation() 的更多信息,请看这里.>

I have a div which I have attached an onclick event to. in this div there is a tag with a link. When I click the link the onclick event from the div is also triggered. How can i disable this so that if the link is clicked on the div onclick is not fired?

script:

$(document).ready(function(){
    $(".header").bind("click", function(){
         $(this).children(".children").toggle();
    });
})

html code:

<div class="header">
    <a href="link.html">some link</a>
    <ul class="children">
        <li>some list</li>
    </ul>
</div>

解决方案

Do this:

$(document).ready(function(){
    $(".header").click(function(){
        $(this).children(".children").toggle();
    });
   $(".header a").click(function(e) {
        e.stopPropagation();
   });
});

If you want to read more on .stopPropagation(), look here.

这篇关于jquery停止子触发父事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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