A quick way to solve the conflict between jquery and other libraries _jquery

Source: Internet
Author: User
Tags anonymous

Generally, when using jquery and other libraries on the same page, the definition of global name $ is the focus of the greatest controversy and conflict. As we all know, jquery uses $ as an alias for the jquery name and uses it for every feature that jquery exposes, but other libraries, most notably prototype, also use the $ name.

1. jquery provides the $.noconflict () utility function to discard the use of the $ identifier so that it is used by other libraries.

The syntax for this function is as follows:
$.noconflict (Jquerytoo)

Returns control of the identifier $ to other libraries, allowing you to mix jquery with other libraries on the page. Once this function has been executed, the functionality of jquery must be invoked using the jquery identifier instead of the $ identifier,

You can also discard jquery identifiers (optional)

This method should be called after jquery has been included, but before the conflict library has been included.


Although the jquery identifier is used, all jquery functionality is available after the $.noconflict () is applied since $ is the alias for jquery. We can define a shorter, but no conflicting jquery alias, such as

var $j = jQuery;

2. Another common idiom is to create a scope environment in which the $ identifier points to the JQuery object, which is a common technique when extending jquery, especially for plug-in authors, who are unlikely to have called $ for page developers. Noconflict () makes any assumptions and of course cannot call this function on its own to avoid damaging the wishes of the page developer,

This idiom is as follows:
(function ($) {}) (JQuery);
(function ($) {})

This section declares a function and encloses it in parentheses, this generates an expression that is the result of a reference to an anonymous function that expects to pass in a single argument and name it $, and in the body of the function, you can refer to anything passed to the function by the $ identifier. Because a parameter declaration takes precedence over any similar named identifier in the global scope, any $ value defined outside the function is replaced by the passed parameter in the function.

(JQuery)

Performs a function call on an anonymous function, passing the jquery object as a parameter


Outside the function, regardless of whether the $ identifier is already defined in prototype or another library, it always points to the JQuery object in the function body.

When using this technique, the externally declared $ is not available in the function body.

3. A variant of the second usage is often used to declare a ready-processing function, thus creating a third syntax,
JQuery (function ($) {
})

This precaution is best applied to the definition of $ when writing reusable components that may be used for pages that already use $.noconflict ().

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.