2016-02-12 62 views
0

首先,例如JS代碼:stopPropagation困境?

$(function(){ 
 
    
 
    $('body').click(function(){alert('body clicked');}); 
 
    
 
    $('body').on('click','.post-header',function(e){ 
 
     
 
     e.stopPropagation(); 
 
     $(this).next('.post-content').slideToggle(); 
 
    
 
    
 
    }) 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<body> 
 

 
     <div class="post-wrapper"> 
 
      <h2 class="post-header">Lorem Ipsum</h2> 
 
      <div class="post-content"> 
 
       <p>Lorem Ipsum is simply dummy text of....</p> 
 
      </div> 
 
     </div> 
 
     
 
    </body>

我的問題是,如果e.stopPropogation()殺死對身體冒泡,怎麼來的單擊事件擊中身體,並觸發處理程序運行幻燈片代碼?

回答

0

參考this answer以前的類似問題,事實證明,使用stopPropagation和委託處理「通常」是無用的。事件在stopPropagation被激活之前已經傳播到正文。

但在上面的例子中,stopPropagation實際上可以防止連接到主體的其他單擊事件處理程序。