【问题标题】:Help with some jQuery in a Wordpress Loop在 Wordpress 循环中帮助一些 jQuery
【发布时间】:2010-08-14 04:46:58
【问题描述】:

我正在制作一个自定义存档页面,该页面将显示帖子列表及其摘录,以及一个按钮,单击该按钮可切换帖子的其余内容。我让它与这段代码一起工作,除了它会切换每个帖子内容,哈哈,因为在循环中他们都得到了你知道我的意思的类吗?

你能帮我找到一个方法吗?必须有一种方法让它在循环中工作,对吗?这是我到目前为止的(失败)代码, 谢谢! `

<div id="more_content">
    <a href="#" class="see_all">More</a>
    <div class="content_hidden" style="display:none;">
        <div class="copy">
        <?php the_content(); ?>
        </div>

        <p class="tags"><?php the_tags(); ?></p>
        <?php comments_template(); ?>
    </div>
</div>

<script type="text/javascript" charset="utf-8">

$('.see_all').click(function() {

if ( $(this).html() == 'More'){
    $('.content_hidden').toggle('slow');

    $(this).html('Less');



}
else if ( $(this).html() == 'Less'){

    $('.content_hidden').slideUp('slow');
    $(this).html('More');

}

return false;

});

`

【问题讨论】:

    标签: javascript jquery wordpress wordpress-theming


    【解决方案1】:

    这应该可行:

    $('.see_all').click(function() {
        if ( $(this).html() == 'More'){
            $(this).parent().find('.content_hidden').toggle('slow');
            $(this).html('Less');
        } else if ( $(this).html() == 'Less'){
            $(this).parent().find('.content_hidden').slideUp('slow');
            $(this).html('More');
        }        
        return false;
    });
    

    引用你的话:

    它会切换每个帖子内容哈哈 因为在循环中他们都得到了 你知道我的意思吗

    您需要做的是在点击链接的每个容器内或相对于.content-hidden 寻址,而不是同时寻址所有这些容器。此外,您可能会在文档中重复 ID(这是不好的、禁止的、禁忌、不要做、无效、违反规范、不起作用等)。所以,你的&lt;div id="more_content"&gt; 应该是&lt;div class="more_content"&gt;

    【讨论】:

    • 啊哈!谢谢,哈哈,我在ID上隔开,很好,谢谢。所以这主要是有效的,例如有两个帖子。第二个可以正常工作,但是第一个在单击时会向下滑动,然后立即向上滑动....
    • 关于为什么第一个 div 会在第二个 div 正常工作时向下滑动然后又向上滑动的任何想法?
    【解决方案2】:

    使用

    <?php the_ID(); ?> 
    

    设置元素的 HTML ID,然后 Javascript 选择要显示的容器的 ID。

    【讨论】:

      猜你喜欢
      • 2010-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-16
      • 1970-01-01
      相关资源
      最近更新 更多