Wordpress中断jQuery背景动画脚本

Wordpress中断jQuery背景动画脚本,jquery,wordpress,Jquery,Wordpress,当包含在Wordpress主题的标题中时,我在一个小的背景动画脚本上得到如下所示的错误(73TypeError),这会破坏脚本。当脚本包含在非Wordpress页面上时,我没有看到任何错误: 以下是脚本: $(function() { $(".bottom").hover( function () { $(this).animate( { backgroundPosition : '0px 35px'} , 300); } , functi

当包含在Wordpress主题的标题中时,我在一个小的背景动画脚本上得到如下所示的错误(73TypeError),这会破坏脚本。当脚本包含在非Wordpress页面上时,我没有看到任何错误:

以下是脚本:

$(function() {
   $(".bottom").hover( function () {
      $(this).animate( {
         backgroundPosition : '0px 35px'}
      , 300); }
   , function () {
      $(this).animate( {
         backgroundPosition : '0px 0px'}
      , 600); }
   ); 
   }
);
有人能告诉我是什么导致了这个错误吗

谢谢


尼克

首先在wordpress中,jQuery的加载应该没有冲突。因此,在这种情况下,您的文件应该是:

var $j = jQuery.noConflict();

$j(function() {
   $j(".bottom").hover( function () {
      $j(this).animate( {
         backgroundPosition : '0px 35px'}
      , 300); }
   , function () {
      $j(this).animate( {
         backgroundPosition : '0px 0px'}
      , 600); }
   ); 
   }
);
其次,就像前面提到的undefined一样,您需要使用
wp-enqueue-script()加载jQuery

为了实现这一点,为了获得最佳实践,请将jQuery放在单独的文件中。比如说bganimation.js

现在要在wordpress中加载js文件,请使用wp-enqueue-script();像这样

function this_is_jquery_example(){
wp_enqueue_script( 'jquery' );;
wp_enqueue_script('myscript', plugin_dir_url(__FILE__) .' bganimation.js');

}
add_action('wp_enqueue_scripts','this_is_jquery_example');

希望这有帮助。

这是因为jQuery没有加载。阅读本文哦,jQuery出现在页面上的脚本列表中,所以我认为它包括在内。我在上面添加了
wp_head()
在我的子主题的header.php文件中,现在我得到了错误
TypeError:“undefined”不是一个函数(计算“$”)
在上面脚本的倒数第二行单击列表中加载的jQuery文件并检查它是否正确加载。@undefined jQuery已正确加载,即使没有添加
,现在我需要解决如何包含下面的无冲突脚本和原始背景位置。js谢谢,这非常有用。正如我在对undefined的评论中提到的,我认为jQuery也包括在内。我听从了你的指示,我创建的bganimation.js路径设置不正确:
http://soteriabrighton.co.uk/wp-content/plugins/home/nickputm/public_html/soteriabrighton.co.uk/wp-content/themes/bp-soteria/bganimation.js?ver=3.4
您是想将其直接包含到主题中还是创建插件?我创建了一个单独的文件调用bganimation.js并将其放入wp content/plugins文件夹哦!那么上面的代码就不适用了。那是一个插件。嗯,我有点困惑。我有原始的backgroundPosition.js,然后是上面的脚本,现在保存在一个名为bganimation.js的单独文件中。我可以把这两个脚本结合起来。那么,这会构成一个可以与您的代码一起工作的插件吗?对不起,这件事做得太慢了。