【问题标题】:Nested Click Event bubbling issue嵌套点击事件冒泡问题
【发布时间】:2012-12-18 20:11:12
【问题描述】:

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

所有图像(子图像、父图像和非父图像)的 Onclick 应显示在一个 diff 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}" />

【问题讨论】:

  • 请也发布您的 HTML 标记。
  • 我怀疑这与事件冒泡无关。我看到你的代码有两件奇怪的事情:(1)从另一个点击事件中绑定一个点击事件; (2) 三种不同的点击状态(“未点击”、“点击”和“点击”)。如果您在 jsfiddle 上发布工作版本(或不工作),也许有人会帮助调试您的代码。
  • 其实有4种点击状态,clicked、not_clicked、click和unclicked。为了我的理智,每个差异状态一个,每个点击事件两个。我认为在点击事件中绑定点击事件是整个问题......我会尝试jsfiddle。

标签: javascript jquery


【解决方案1】:

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

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

更多信息: 1.jquery 2.javascript

【讨论】:

    猜你喜欢
    • 2018-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-26
    • 1970-01-01
    • 2015-12-10
    • 2011-03-19
    • 2023-04-02
    相关资源
    最近更新 更多