【问题标题】:jQuery how to stop triggered click event from bubblingjQuery如何阻止触发的点击事件冒泡
【发布时间】:2014-07-11 07:19:16
【问题描述】:

我有这个代码:

$('.panel-select').click(function() {
  $(this).children('input').trigger('click');
});

但我收到 RangeError:超出最大调用堆栈大小。 我搜索了一些,发现事件在 DOM 中冒泡。

我试图阻止它,但我没有成功。 我试过这个:

$('.panel-select').click(function(e) {
  e.stopPropagation();
  $(this).children('input').trigger('click');
});

但它不起作用。我该怎么做?

【问题讨论】:

    标签: jquery event-bubbling stoppropagation


    【解决方案1】:

    你需要有一个绑定到子元素的事件:

    $('.panel-select').click(function() {
       $(this).children('input').trigger('click');
    });
    
    // try adding to the below event not on the parent.
    
    $('.panel-select input').click(function(e) {
        e.stopPropagation();
    });
    

    【讨论】:

    • @JohanDahl 很高兴这对您有所帮助。
    猜你喜欢
    • 2011-07-03
    • 1970-01-01
    • 2011-05-30
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 2011-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多