【发布时间】:2013-07-05 00:32:14
【问题描述】:
我有一组“问题/答案” div 配对,其中“答案” div 被隐藏并在“问题” div 被点击时显示。集合中的第一对应该加载已经打开的答案。在 wordpress 之外,这可以按预期工作:http://jsfiddle.net/Y724r/1/
当我将相同的代码放入 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');
});
【问题讨论】:
标签: jquery wordpress loops toggle css-selectors