为什么写这篇文章? 因为之前在wordpress里面用jquery的时候没注意,这次因为要优化网站把没用的插件删了,结果发现有的jquery功能失效了,调试了许久才发现可能是jQuery没有定义,知道原来在这个页面没有加载jquery,后来在网上查找加载jquery的方法,加上之后问题解决。
下面是调用jQuery的几种方法:
1.自己从网上下载挂在自己网站中。 <script type=”text/javascript” src=”<?php bloginfo('template_directory'); ?>/js/jquery.js”> </script> 2.直接调用谷歌 Google 的 API库 <script type=”text/javascript” src=”http://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js”> </script> 3.直接调用WP自带的 jQuery <?php wp_enqueue_script('jquery’); ?> 但要注意的是: wp_enqueue_script('jquery'); 必需加在 wp_head(); 的前面, 而且 js 的写法是: jQuery(document).ready(function($) { // $() will work as an alias for jQuery() inside of this function }); 这样 jQuery() 包里的所有 $() 才能被正确识别. 大家的习惯写法也: $(document).ready(function() { … }); 需要改为 jQuery(document).ready(function($) { … }); 这是为了 noConflict(); 另外, 把全部简写的“$” 还原回,使用“jQuery” 也行.