javascript - 嵌套点击事件冒泡问题

标签 javascript jquery

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

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

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

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

有人看到解决这个问题的好方法吗?

// 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;
            });
        }
    }
});
<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}" />

最佳答案

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

$('.item-button').click(function(event) {
     event.stopPropagation();
     // your code...

更多信息: 1. jquery 2. javascript

关于javascript - 嵌套点击事件冒泡问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14156287/

相关文章:

javascript - 如何在 Wordpress 中激活这个 HTML 类

javascript - jQuery 动画函数在第二次调用时重置滚动

javascript - NaN 错误 - 处理字符串和整数

javascript - 使用jspdf在pdf中添加图像

javascript - 在调用 `connectedCallback` 之前,自定义元素的 `disconnectedCallback` 是否可以多次调用?

javascript - 自定义点击计数器

javascript - 从 json 字符串解析图像

javascript - 使用正则表达式掩码实时控制浮点输入

javascript - 如何使用 JavaScript 截取 div 的屏幕截图?

javascript - 如何在选择列表项时重新加载 iframe