2013-05-13 89 views
0

与我的FTP客户端混淆后,我的Wordpress'header.php被删除,所以我需要重新编码它,以前工作的jQuery脚本停止工作,但我找不到问题。jQuery Accordion停止工作

这是一个手风琴脚本,可以有多个部分同时开放:

<script type="text/javascript"> 

$(document).ready(function() { 
     $(".post-list").addClass("ui-accordion ui-accordion-icons ui-widget ui-helper-reset") 
      .find("h3.entry-title") 
      .addClass("ui-accordion-header ui-helper-reset ui-state-default ui-corner-top ui-corner-bottom") 
      .hover(function() { $(this).toggleClass("ui-state-hover"); }) 
      .click(function() { 
       $(this) 
       .toggleClass("ui-accordion-header-active ui-state-active ui-state-default ui-corner-bottom") 
       .next().toggleClass("ui-accordion-content-active").slideToggle(); 
       return false; 
     }) 
     .next() 
     .addClass("ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom") 
     .hide(); 

     var d= 0; 
     $('.post').each(function() { 
      $(this).delay(d).fadeIn(300); 
      d += 100; 
     }); 
}); 

</script> 

它应该找到H3的,添加类和隐藏以下div的。我把它放在我的header.php的头部区域,所有必要的库都被加载,但不知怎的,脚本似乎根本不起作用。

任何人都可以找到一个错误或建议什么可能是错误的脚本的实施?

注意:我试着在本地虚拟网站上完全相同的脚本,它的工作原理。

谢谢!

+0

可能缺少jQuery的css文件 – 2013-05-13 23:21:46

+0

你检查控制台,你得到任何错误? – ocanal 2013-05-13 23:22:21

+0

它说“找不到变量:$” – R4ttlesnake 2013-05-13 23:25:13

回答

0

如果您在控制台上发现“无法找到变量:$”错误,这意味着jQuery未加载或冲突任何其他库。

首先检查控制台上是否存在window.$window.jQuery

,并尝试使用jQuery代替$

<script type="text/javascript"> 
$.noConflict(); 
jQuery(document).ready(function() { 
     jQuery(".post-list").addClass("ui-accordion ui-accordion-icons ui-widget ui-helper-reset") 
      .find("h3.entry-title") 
      .addClass("ui-accordion-header ui-helper-reset ui-state-default ui-corner-top ui-corner-bottom") 
      .hover(function() { jQuery(this).toggleClass("ui-state-hover"); }) 
      .click(function() { 
       jQuery(this) 
       .toggleClass("ui-accordion-header-active ui-state-active ui-state-default ui-corner-bottom") 
       .next().toggleClass("ui-accordion-content-active").slideToggle(); 
       return false; 
     }) 
     .next() 
     .addClass("ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom") 
     .hide(); 

     var d= 0; 
     jQuery('.post').each(function() { 
      jQuery(this).delay(d).fadeIn(300); 
      d += 100; 
     }); 
}); 

+0

对不起,我没有使用'window。$ '等等,我尝试了代码,但仍然没有奏效。如果我将原始代码放在一个单独的.js文件中,并使用array('jquery')'将其排入到functions.php中,它就可以工作。但我仍然无法弄清楚为什么它以前工作? – R4ttlesnake 2013-05-13 23:48:57

+0

尝试[this](http://stackoverflow.com/a/12225872/603127)它可能工作。正如你所说你的脚本代码在jQuery加载之前工作。和似乎没有任何办法使非文件js代码工作后jQuery加载除[this](http://stackoverflow.com/a/12225872/603127) – ocanal 2013-05-14 00:12:14

+0

我想我会坚持的方法与一个单独的JS文件,似乎是最安全的方式。但感谢您的帮助和wind.onload解释! – R4ttlesnake 2013-05-14 11:35:57