2013-07-07 14 views
0

我有一組'question/answer'div pairings,'answer'div隱藏並顯示'question'div被點擊的時間。該集合中的第一對應該加載已經打開的答案。在wordpress之外,這個按預期工作:http://jsfiddle.net/Y724r/1/:第一個孩子切換功能不像預期的那樣在wordpress循環中運行

當我把相同的代碼放入wordpress循環時,最後一個問題加載打開,而不是第一個:http://www.htcaz.com/web/?page_id=5(關於HTC的配對是'答案」 DIV應該是負荷開放,但這樣做,而不是最後一個)

循環:

<?php $subpages = new WP_Query("post_parent=5&post_type='page'&orderby=ID&order=ASC"); 
if($subpages->have_posts()) : while($subpages->have_posts()) : $subpages->the_post(); ?> 
    <div class="post"> 
     <div class="question"> 
      <?php the_title(); ?> 
     </div> 
     <div class="answer"> 
      <?php the_content(); ?> 
     </div> 
    </div> 
<?php endwhile; endif; ?> 

的jQuery:

jQuery(document).ready(function() { 
jQuery(".answer").hide(); 
jQuery(".question").click(function() { 
    jQuery(".answer").not(jQuery(this).next(".answer")).hide(); 
    jQuery(this).next(".answer").toggle(); 
}); 
jQuery('.question:first-child').trigger('click'); 
}); 

回答

0

刪除此

jQuery('.question:first-child').trigger('click');

jQuery('.question:eq(0)').trigger('click');

jQuery(document).ready(function() { 
jQuery(".answer").hide(); 
jQuery(".question").click(function() { 
    jQuery(".answer").not(jQuery(this).next(".answer")).hide(); 
    jQuery(this).next(".answer").toggle(); 
}); 
jQuery('.question:eq(0)').trigger('click'); 
}); 

eq(n)jQuery選擇通過索引取得元素沒有。所以在頁面加載,你必須trigger在第一.question DIV的click事件

Here is your fiddle

eq(n)

+0

是你搖滾!並特別感謝包括進一步閱讀的文檔鏈接:) – pixeloco

+0

歡迎:) –

相關問題