jquery prevents event bubbling and its solution

一个新手
Release: 2017-09-13 10:05:58
Original
2004 people have browsed it


Live event for dynamically adding tags

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

html

init html

Copy after login
jq
$(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 = 'new html 1'; $('#box').append(html); }); $('#box').click(function() { console.log('box emit'); }); });
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!