jQuery无法在WordPress中运行

我有一些jQuery脚本可以在这里找到: http : //jsfiddle.net/RUqNN/45/

当我将这个脚本合并到我的wordpress模板中时,我无法工作。

我检查了jquery源链接,重写了代码,没有任何效果。

      $(document).ready(function() { $(".move").toggle( function() { $("#teetimetag").animate({ right: "0px" }, "normal"); },function() { $("#teetimetag").animate({ right: "-200px" }, "normal"); }); });     

您的网站提供以下javascript错误:’$不是函数’。 看起来jQuery与其他一些库冲突。 这可以通过调用jQuery.noConflict()并用’jQuery’替换所有$来解决。 代码如下:

 jQuery.noConflict(); jQuery(document).ready(function() { jQuery(".move").toggle( function() { jQuery("#teetimetag").animate({ right: "0px" }, "normal"); },function() { jQuery("#teetimetag").animate({ right: "-200px" }, "normal"); }); }); 

你应该在你的模板functions.php文件中注册你的js文件:

 wp_register_script('jquery', 'http://code.jquery.com/jquery-1.7.2.min.js'); 

http://codex.wordpress.org/Function_Reference/wp_register_script

作为一般规则,除非使用其中一个快捷方式,否则不应将$ variable用于jQuery 。 以下是如何快捷方式jQuery安全使用$变量的示例:

 jQuery(function ($) { /* You can safely use $ in this code block to reference jQuery */ }); 

要在wordpress加载jquery,请使用以下两个版本之一。 在Plugin Page或在function.php包含代码

 function include_jQuery() { if (!is_admin()) { wp_enqueue_script('jquery'); } } add_action('init', 'include_jQuery'); 

绝对可以使用任何有意义的函数名来代替include_jQuery

或者,您可以使用以下代码:

 function include_jQuery() { if (!is_admin()) { // comment out the next two lines to load the local copy of jQuery wp_deregister_script('jquery'); wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js', false, '1.8.3'); wp_enqueue_script('jquery'); } } add_action('init', 'include_jQuery'); 

您也可以从我亲自学习上述技术的地方获得此链接。 非常感谢Ericm Martin!