Jquery1.9+ removes the alternative to live after _jquery

Source: Internet
Author: User

According to the official jquery description, the live method has not been recommended for use in 1.7, and this method was removed in 1.9. It is recommended that the on method be used in future code instead.

The on method can accept three parameters: the event name, the trigger selector, and the event function.

A special note is 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, and you can use it to achieve live effects.

For example, I already have a div with ID parent in my HTML document, and I'm going to add a son span within this div, and then I'll bind an event for this span, so I need to write this:

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

The trigger selector is actually the $ (e.target) of the event argument within the JQ. Is (selector), which is triggered only if the trigger object matches the trigger selector. This is done using the mechanism of event bubbling, the original live is also using the bubble mechanism so since on can be implemented then live is not necessary, but for the sake of compatibility let it from 1.7 live to 1.9.

This article also has nothing to do, and then use this function to do some meaningful things to demonstrate it ~ in the low version of IE in the mouse click on the button will appear 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 a lower version of the browser. Therefore, it is effective for focus to use live for the lower version of the browser. Before the jQuery1.9 version we can write this:

$ ("a"). Live ("Focus", function () {
this.blur ();
}); 
After jQuery1.9 since live was deleted, it should be written like this:
$ (document). On ("Focus", "a", function () {
this.blur ();

Also pay attention to a problem, if it is from live to the writing on the writing do not forget to adjust the call chain. Because live's return value is an event-triggered object, using 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");

Note that the last find ("div"), the other is no problem.

Here's an official note.

http://jquery.com/upgrade-guide/1.9/

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.