Home > Article > Web Front-end > jquery prevents event bubbling and its solution
Note: jquery no longer supports live events after version 1.8
In Problems encountered in actual projects, dynamically added tags
Live prevents bubbling from happening. Neither return false nor e.stopPropagation() can prevent bubbling from happening
The following is an example I summarized
<p id="box"> <a href="javascript:;" class="delete">init html</a> </p> <button id="add">add html</button>
$(function() { // 用click事件 $(document).click( function(event) { console.log('click'); event.stopPropagation(); }); // 用delegate事件 $(document).delegate('.delete','click', function(event) { console.log('delegate'); event.stopPropagation(); }); // 用live事件 $('.delete').live('click', function(event) { console.log('live'); event.stopPropagation(); //return false; }); // 新添加的a标签 $('#add').click(function() { var html = '<a href="javascript:;" class="delete">new html 1</a>'; $('#box').append(html); }); $('#box').click(function() { console.log('box emit'); }); });
Solution:
We know that event.stopPropagation() is effective for click to prevent bubbling , then you can bind the click event to the newly dynamically added label.
The above is the detailed content of jquery prevents event bubbling and its solution. For more information, please follow other related articles on the PHP Chinese website!