jquery1.9+ to remove live alternatives

Source: Internet
Author: User

According to the official jquery description, the live method is not recommended in 1.7, and this method is removed in 1.9. It is also recommended to use the On method in future code instead.
The on method can accept three parameters: event name, trigger Selector, event function.

It is important to note that the trigger selector in the middle of the on method is the class name, ID, or element name of the HTML element you are going to add, which you can use to achieve the effect of live.

For example, my HTML document already has a DIV with the ID of parent, and I'm going to add a span of class to son inside this div, and then I bind an event for this span, so I need to write:

$ (' #parent '). On (' click ', '. Son ',function () {alert (' Test ')});

This trigger selector actually determines the $ (e.target) of the event parameter within JQ. Is (selector), only the Trigger object match trigger selector is triggered. This is the use of event bubbling mechanism to complete, the original live is also using the bubbling mechanism, so since on can be achieved then live there is no need to exist, just for the sake of compatibility let it from 1.7 live to 1.9.
This article also does not have any content, next will use this function to do some meaningful things to show the next ~ in the low version of IE, a tag will appear when the mouse press the dotted border, which is caused by focus. We can solve this problem simply by doing something in the global event. Focus is not bubbling in modern browsers, but it can be bubbling in the lower version of the browser. Therefore, it is valid for focus to use live in a low-version browser. Before the jQuery1.9 version we can write:

$ ("a"). Live ("Focus",function () {   

jQuery1.9 since live was deleted, it should be written like this:

$ (document). On ("Focus", "a",function () {   

Also pay attention to the question, if you change from the writing of live to on the wording don't forget to adjust the call chain. Because the return value of Live is an event-triggered object, the use of on is on the container object.

jquery1.9-$ ("#panel"). Find ("div"). Live ("Click",function () {  alert ("X");}). AddClass ("X"); //jquery1.9+$ ("#panel"). On ("click", "div",function () {alert ("X");}).  Find ("div"). addclass ("X"); 

jquery1.9+ to remove live alternatives

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.