In this article, we will explain several methods to call jQuery. Of course, they are all for WordPress. Please choose as you like:
The only difference between WP jQuery and the original version is that it is added to the last line.
The code is as follows: |
Copy code |
JQuery. noConflict (); |
This noConflict () is used for compatibility with other libraries, such as Prototype, MooTools, or YUI.
The first method is to directly search for jQuery effects on the Internet and then call them directly:
The code is as follows: |
Copy code |
<Script type = "text/javascript" src = "<? Php bloginfo ('Template _ directory ');? >/Js/jQuery. js "> </script> |
2. calling using Google's API library seems like many jQuery calls are like this. Many plug-ins use Google API to replace WP's default jQuery and use Google's powerful server to speed up calling and loading.
The code is as follows: |
Copy code |
<Script type = "text/javascript" src = "http://ajax.googleapis.com/ajax/libs/jQuery/1.2.3/jQuery.min.js"> |
3. Directly call the built-in jQuery library of WordPress:
<? Php wp_enqueue_script ('jquery ');?>
Note that this sentence must be added before wp_head!
In addition, the js file is written as follows:
The code is as follows: |
Copy code |
JQuery (document). ready (function ($ ){... }); |
In this way, all $ () in the jQuery () package can be correctly identified.
Of course, you can also use the following method:
The code is as follows: |
Copy code |
$ (Document). ready (function (){... }); To: JQuery (document). ready (function ($ ){... }); |
Next we will share the best way to load the jQuery library in wordpress. Add the following code to the header. Php file in the topic of Tom. You can load the local jQuery library when the Google CDN library fails to be obtained:
The code is as follows: |
Copy code |
<Script type = "text/javascript" src = "// ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"> </script> <Script type = "text/javascript"> window. jQuery | document. write ('<script type = "text/javascript" src = "<? Php echo get_template_directory_uri ();?> /Jquery. min. js "> x3C/script> ') </script> |
In this way, the best way to load the jQuery library is to load the jQuery library file in the local topic after the online loading fails. Now, Google CDN's jQuery library version seems to be more than version 8.2, and laruence is using the jQuery library file of version 1.5.2. At noon, we downloaded the jQuery library file version 1.8.2, which is a little bigger than version 5.2. Therefore, to save resources and ensure compatibility with topics. It is better to load the jQuery library file required by the topic.