【问题标题】:Clicking child element fires parent click event单击子元素会触发父单击事件
【发布时间】:2015-06-15 19:15:09
【问题描述】:

我知道这个问题已被问过几次,但我似乎无法解决这个问题。当孩子点击时,我不希望触发父事件。我有以下内容:

<div class="tabItem">
    <span> Item 1</span>
    <div class='contentBody'>Item 2 Body</div>
</div> 

还有下面的jquery:

$(document).ready(function() {

            $('.tabItem').each(function(index){                             

                $(this).attr('id', 'tabIndex'+index);               
                var contentDiv = $(this).find('div').first();
                var position = contentDiv.position();
                var right = $(window).width() - position.left - contentDiv.width() ;
                var top = (position.top - 18) ;
                var contentId = 'tabContent' + index;           

                $(contentDiv).css( "display", "none" ).css({top: top, left: 200}).attr('id', contentId);                

                $(this).on('click',  function() {               

                    $(this).removeClass('tabItem').addClass('tabItemSelected');                                         

                     $(currentTab).removeClass('tabItemSelected').addClass('tabItem');  

                    if($('#' + currentContentBody).is(":visible")){
                        $('#' + currentContentBody).toggle( "slide" ); // close the last tab
                    }
                    if($(contentDiv).is(":hidden")){
                        $(contentDiv).toggle( "slide" );    

                    }           
                     currentTab = $(this);
                     currentContentBody = $(contentDiv).attr('id');

                });             
            });

  });

点击事件适用于类tabItem 的父级。但是当我单击子 div 时,父事件文件。这是一个工作示例jsfiddle

【问题讨论】:

标签: jquery click


【解决方案1】:

您需要在孩子的事件处理程序上调用event.stopPropagation()。这将防止事件冒泡到父元素。

对此要非常小心,因为如果您不小心,您可以快速切断所有点击事件,以免冒泡到父级。这显然不是你想要的。

【讨论】:

  • 没错。这可以防止子点击传播/冒泡到父类。
【解决方案2】:

另一种可能的解决方案是将click 事件绑定到span

$(this).find('span').on('click', function(e) {

演示:https://jsfiddle.net/lmgonzalves/asqwnr8v/3/

注意:您还需要在click 事件中将一些$(this) 修复为$(this).parent()

【讨论】:

    【解决方案3】:

    只需将您的点击处理程序更改为:

            $(this).on('click',  function(e) {               
    
                $(this).removeClass('tabItem').addClass('tabItemSelected');                                         
    
                 $(currentTab).removeClass('tabItemSelected').addClass('tabItem');  
    
                if($('#' + currentContentBody).is(":visible")){
                    $('#' + currentContentBody).toggle( "slide" ); // close the last tab
                }
                if($(contentDiv).is(":hidden")){
                    $(contentDiv).toggle( "slide" );    
    
                }           
                 currentTab = $(this);
                 currentContentBody = $(contentDiv).attr('id');
    
                e.stopPropagation();
    
            });
    

    重要的变化在哪里:

    $(this).on('click', function(e) {

    e.stopPropagation();

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-07
      • 2013-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-02
      • 1970-01-01
      相关资源
      最近更新 更多