2014-03-04 102 views
0

我想旁邊添加jQuery腳本:jsfiddle examplejQuery函數 - 不工作

但是,我只得到李的靜態列表,而不是淡入列表作爲它的的jsfiddle例子。

我正在使用「underscores.me」框架,我試圖將腳本添加到「customizer.js」文件中,我也嘗試將腳本添加到頁面,有一次我試着在ul標籤之上,並且其他時間我嘗試了下ul標籤 - 但沒有,我仍然得到李的靜態列表。

在我的網站的源代碼,我看到:

<script type='text/javascript' src='http://*****/wp-includes/js/jquery/jquery.js?ver=1.10.2'></script> 
<script type='text/javascript' src='http://****/wp-includes/js/jquery/jquery-migrate.min.js?ver=1.2.1'></script> 

所以jQuery的負載好(我編輯在這裏* *標誌,而不是網站的網址)

我的代碼(這些代碼是在同一頁上):

<script> 
jQuery(document).ready(function showTheList() { 
var i = 1; 
function showOne() { 
if (i === 1) $('.ul li').hide(); 
$('ul li').eq(i - 1).delay(1000).fadeIn(1000, function() { 
$('ul li').eq(i - 1).fadeOut(100, function() { 
if (++i > 4) i = 1; 
showOne(); 
}); 
}); 
} 
showOne(); 
}); 

</script> 
<ul class='ul'> 
<li>Hello</li> 
<li>Out</li> 
<li>There</li> 
<li>World!</li> 
</ul>   
<script>      
$(document).ready(showTheList()); 
</script> 

那麼......爲什麼列表不像示例那樣淡入?

回答

0

您是否在$(document).ready()處理程序中調用showTheList函數?如果你在你的元素準備好之前運行它,它什麼都不會做。

+0

嘿謝謝,我編輯了我的問題。我不用jquery很多工作,所以也許我做了一些有趣和錯誤的事情...檢查你是否可以:)謝謝 – Oshrib

+0

你打電話給準備處理程序錯誤。你需要使用'jQuery(document).ready(function(){//你的代碼});'。例如:'$(document).ready(function(){showTheList();});'。此外,您可以從ready handler中刪除上層腳本(函數定義),並保持原樣。有關詳情,請訪問http://api.jquery.com/ready/ –

0

解決辦法:

 <ul class='aa'> 
      <?php if (have_posts()) : ?> 

      <?php /* Start the Loop */ ?> 

      <?php query_posts('cat=3&orderby=date&order=ASC'); ?> <!-- post from '3' category --> 

      <?php while (have_posts()) : the_post(); ?> 


      <li><a href="<?php the_field('news-link') ?>" rel="nofollow" target="_blank"> <?php the_title(); ?></a></li> 

       <?php endwhile; ?> 

       <?php endif; ?> 
     </ul> 


<script type="text/javascript"> 
function InOut(elem) 
{ 
elem.delay() 
    .fadeIn(1) 
    .delay(100) 
.fadeOut(
      function(){ 
      if(elem.next().length > 0) // if there is a next element 
       {InOut(elem.next());} // use it 
      else 
       {InOut(elem.siblings(':first'));} // if not, then use go back to the first sibling 

      } 
     ); 
} 





jQuery(document).ready(function() { 
jQuery('.aa li').hide(); 
InOut(jQuery('.aa li:first')); 
}); 





</script>  

使用jquery WordPress的頁面上,最好的辦法是在它之前加入jQuery和使用(文件)。就緒,而不是$大關。在這個例子中,在答案中,我使用了其他腳本,然後是我添加到我的問題的腳本,但它的想法相同。