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> |