Avoid jquery name Conflicts noconflict () method _jquery

Source: Internet
Author: User
Tags jquery library

As we all know, in the jquery syntax, the $ symbol is shorthand for jquery. In some cases, however, you might want to introduce additional JavaScript libraries (such as prototype) on the same page. Because of the short convenience, many libraries also use the $ symbol. To avoid name collisions, jquery provides a noconflict () method to solve this problem. Calling this method gives control of the $ identifier to other libraries.

General mode:

<script src= "Prototype.js" ></script>//1. Libraries outside of jquery (such as prototype)
<script src= "Jquery.js" > </script>//2. Contains the JQuery library to obtain the right to use the $
<script>
  jquery.noconflict ();//3. Call Noconflict () method, let out $, To cede control to the first libraries included
</script>

When you need to use the JQuery method when you give up $ control, you cannot use $ to invoke it, using jquery. Or you can replace the $ symbol by defining a new name.

var jq=jquery.noconflict ();

Another trick is to use $ in the Ready () method. Its callback function can receive a parameter, which is the jquery object itself, and can rename jquery to $, which is not a conflict.

Jquery. (document). Ready (function ($) {
  //This can be used normally $ 
})

Of course, the shorthand method is also possible

JQuery (function ($) {
  //Here you can use $ 
})

The above is the entire content of this article, I hope that you learn jquery to avoid conflict methods help.

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.