Solve the problem of repeated conflicts between jQuery library usage $ and other JS Library trials $.

Source: Internet
Author: User
Tags jquery library

Sometimes jQuery and other javascript need to be used at the same time, but it may be because jquery and other js both use $ as the abbreviation, which may cause repeated js conflicts and js errors, javascript on the page cannot run normally. To make jQuery and other javascript libraries coexist, there are three methods (both from the official website ):

1. Overwrite $ Function, replace $ with jQuery

The code is as follows: Copy code
<Script src = "otherjs. js"> </script>
<Script src = "jquery. js"> </script>
<Script type = "text/javascript">
<! --
// This sentence cannot be fewer
JQuery. noConflict ();
 
// Use jq -- Change $ to jQuery
JQuery (document). ready (function (){
JQuery ("div"). hide ();
});
 
// Use otherjs with $ (...), which remains unchanged.
$ ('Otherid'). hide ();
 
// -->
</Script>

[This will restore $ to the original library and use "jQuery" instead of "$" in the following code "]

2. Let jquery replace $ with other abbreviations. For example, $ zhoumanhe replaces the original $

The code is as follows: Copy code
<Script src = "otherjs. js"> </script>
<Script src = "jquery. js"> </script>
<Script type = "text/javascript">
<! --
// This sentence cannot be less, and $ is redefined as $ zhoumanhe
Var $ zhoumanhe = jQuery. noConflict ();
 
// Use jq -- Change $ to jQuery
$ Zhoumanhe (document). ready (function (){
$ Zhoumanhe ("div"). hide ();
});
 
// Use otherjs with $ (...), which remains unchanged.
$ ('Otherid'). hide ();
 
// --> </Script>

[This is equivalent to $ zhoumanhe instead of $. Of course, other characters can be used .]

3. The $ of jq on the page is used too much. You do not want to change it too much and want to keep $. Use the following method:

The code is as follows: Copy code

<Script src = "otherjs. js"> </script>
<Script src = "jquery. js"> </script>
<Script>
// This sentence cannot be fewer
JQuery. noConflict ();
 
// Put all your code in your document ready area
// Use jq -- place the jq code in jQuery (document). ready (). The jq code in it still uses $
JQuery (document). ready (function ($ ){
// You can still use $
$ ("Div"). hide ();
});
 
// Use otherjs with $ (...), which remains unchanged.
$ ('Otherid'). hide ();
</Script>

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.