首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >嵌套Click事件冒泡问题

嵌套Click事件冒泡问题
EN

Stack Overflow用户
提问于 2013-01-04 19:28:14
回答 1查看 3.1K关注 0票数 4

我正在处理一个嵌套的单击事件(有大量的图像,一些有子图像,一些没有)。

对于所有的图像(子项、父项和非父项),它们都应该显示在一个不同的css div中,然后如果再次单击,就会无限期地删除它们自己。

到目前为止,这段代码适用于父图像和非父图像,但是冒泡是使子图像显示父图像。

如果我在子函数之前返回false来停止冒泡,它将破坏数组中更低的父图像和非父图像的功能(子图像在新的弹出css中,而不是列表中),但是如果我不停止冒泡,嵌套的click事件将不会运行。

有没有人看到解决这个烂摊子的好办法?

代码语言:javascript
复制
// Click an image, and it will change it in the demo css class panel
$('.item-button').data('status','not_clicked');
$('.item-button').click(function(event) {
    var layerID = $(this).attr('data-layer'); //these do not correspond to actual layers of nesting.
    var itemID = $(this).attr('data-item');
    var itemmulti = $(this).attr('data-multi'); //this value def works to recognize parent from other non-parent items (not children).
    var clayerID = $(this).attr('data-clayer'); 
    var citemID = $(this).attr('data-citem');   //child item 1.

    if(( $(this).data('status') == 'clicked') || itemID == 0) {
        var imageSrc = $(this).parent().find('img').attr('id');
        $(layerID).attr('src', imageSrc);
    } else  {
        $(this).data('status','clicked');
        var imageSrc = $(this).parent().find('img').attr('id');
        $(layerID).attr('src', imageSrc);

        if (itemmulti != 0) {
            //begin headache.
            document.getElementById('child-' + itemID).style.display = "inline-block";

            //this is where a separate click function for children items begins.
            $('.item-sub1').data('status','unclicked');
            $('.item-sub1').click(function(event) {

            if(( $(this).data('status') == 'click') || citemID == 0) {
                var imageSrc = $(this).parent().find('img').attr('id');
                $(selector).attr('src', imageSrc);
            } else {
                $(this).data('status','click');
                var imageSrc = $(this).parent().find('img').attr('id');
                $(clayerID).attr('src', imageSrc);
            }
            return false;
            });
        }
    }
});
代码语言:javascript
复制
<img button title id alt />
<input radio id="layer-{ID}-{item.ID}-radio" name="layer-{ID}" value="{item.ID}" class="item-button" data-multi="{item.PARENT}" data-layer="{ID}" data-item="{item.ID}" />
<!-- IF item.CHILD1 != 0 -->
<div id="child-{item.ID}" style="width: 300px; position: absolute; display: none;">
    <img button here title data-layer="{item.CLAYER1}" data-item="{item.CHILD1}">
    <input radio id="layer-{item.CLAYER1}-{item.CHILD1}-radio" name="layer-{item.CLAYER1}" value="{item.CHILD1}" style="display: none;" class="item-sub1" data-clayer="{item.CLAYER1}" data-citem="{item.CHILD1}" />
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-04 19:56:16

我认为您需要做的是使用stopPropagation()方法来防止事件在DOM树上冒泡,并防止任何父处理程序收到事件通知。

代码语言:javascript
复制
$('.item-button').click(function(event) {
     event.stopPropagation();
     // your code...

更多信息: 1. jquery 2. javascript

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14156287

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档