【问题标题】::first-child toggle not functioning as expected in wordpress loop:first-child 切换在 wordpress 循环中未按预期运行
【发布时间】: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


    【解决方案1】:

    删除这个

    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上的点击事件

    Here is your fiddle

    eq(n)

    【讨论】:

    • 是的,你摇滚!并特别感谢您包含文档链接以供进一步阅读:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-13
    • 2015-03-31
    • 2021-04-04
    • 1970-01-01
    • 2011-05-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多