Detailed explanation of the use of jQuery live event
Overview
jQuery attaches an event handling function to all matching elements, even if the element is added later.
This method is basically a variant of the .bind() method. When using .bind(), elements matched by the selector will have an event handler attached to them, while elements added later will not. You need to use .bind() again for this. For example,
<body> <div class="clickme">Click here</div> </body>
can bind a simple click event to this element:
$('.clickme').bind('click', function() { alert("Bound handler called."); });
When the element is clicked, a warning box will pop up. Then, imagine that another element is added after this.
$('body').append('<div class="clickme">Another target</div>');
Although this new element can also match the selector ".clickme", since this element is added after calling .bind(), clicking on this element will have no effect.
.live() provides a method for this situation. If we bind the click event like this:
$('.clickme').live('click', function() { alert("Live handler called."); });
and then add a new element:
$('body').append('<div class="clickme">Another target</div>');
Then click on the new element, he can still trigger the event handler.
Event Delegation
The.live() method can be effective for an element that has not been added to the DOM due to the use of event delegation: the event handling function bound to the ancestor element can Respond to events triggered on descendants. The event handler passed to .live() will not be bound to the element, but will be treated as a special event handler and bound to the root node of the DOM tree. In our example, when a new element is clicked, the following steps will occur:
Generate a click event and pass it to
for processing
Since there is no event handling function Directly bound to Events continue to bubble up to the root node of the DOM tree. This special event handler is bound to it by default. Execute the special click event handling function bound by .live(). This event handling function first detects the target of the event object to determine whether it needs to continue. This test is implemented by checking whether $(event.target).closest('.clickme') can find a matching element. If a matching element is found, the original event handler is called. Since the test in step 5 above will only be done when the event occurs, elements added at any time can respond to this event. Additional Notes Although .live() is useful, due to its special implementation, it cannot simply replace .bind() in any case. The main differences are: In jQuery 1.4, the .live() method supports custom events and also supports all JavaScript events. In jQuery 1.4.1, there is even support for focus and blue events (mapped to the more appropriate, bubbling focusin and focusout). In addition, in jQuery 1.4.1, hover (mapped to "mouseenter mouseleave") is also supported. However, in jQuery 1.3.x, only supported JavaScript events and custom events are supported: click, dblclick, keydown, keypress, keyup, mousedown, mousemove, mouseout, mouseover, and mouseup. .live() does not fully support elements found through DOM traversal. Instead, you should always use the .live() method directly after a selector, as mentioned in the previous example. When an event handling function is bound with .live(), to stop executing other event handling functions, this function must return false. Simply calling .stopPropagation() will not accomplish this. Refer to the .bind() method for more information about event binding. In jQuery 1.4.1, .live() can accept multiple events, separate events with spaces, and provide functions similar to .bind(). For example, we can "live" bind both the mouseover and mouseout events, like this: In jQuery 1.4.3: You can bind one or more event types to StringAnd function data mapping to execute them In jQuery 1.4, the data parameter can be used to pass additional information to the event handler function. A good use is to deal with problems caused by closures. See the discussion of .bind() for more information. In jQuery 1.4, the live event can be bound to the "context" DOM element instead of the default document root. To set this context, we use the jQuery() function's second argument on a single DOM element (rather than a jQuery collection or selector). 参数 type,[fn]String,FunctionV1.3 type:一个或多个事件类型,由空格分隔多个事件。 fn:要从每个匹配元素的事件中反绑定的事件处理函数 type,[data],falseString,Array,boolV1.4 type:一个或多个事件类型,由空格分隔多个事件。 data:传递给事件处理函数的附加参数 false:设置为false会使默认的动作失效。 eventStringV1.4.3 一个或多个事件类型的字符串和函数的数据映射来执行他们 示例 描述: 点击生成的p依然据有同样的功能。 HTML 代码: jQuery 代码: 描述: 阻止默认事件行为和事件冒泡,返回 false jQuery 代码: 描述: 仅仅阻止默认事件行为 jQuery 代码: 这是代码,用live绑定的事件不执行,但是创建的元素能添加进去。这是为什么 你的_html是字符串啊,你是不是要点击li触发事件?$('.hoverme').live('mouseover mouseout', function(event) {
if (event.type == 'mouseover') {
// do something on mouseover
} else {
// do something on mouseout
}
});
$("a").live({
click: function() {
// do something on click
},
mouseover: function() {
// do something on mouseover
}
});
$('div.clickme', $('#container')[0]).live('click', function() {
// Live handler called.
});
<p>Click me!</p>
$("p").live("click", function(){ $(this).after("<p>Another paragraph!</p>"); });
$("a").live("click", function() { return false; });
$("a").live("click", function(event){ event.preventDefault(); });
那应该是$('li').live('click',function(){....})
The above is the detailed content of Detailed explanation of the use of jQuery live event. For more information, please follow other related articles on the PHP Chinese website!

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Chinese version
Chinese version, very easy to use
