【问题标题】:JQuery & Wordpress - Hide multiple divs inside unique ID?JQuery 和 Wordpress - 在唯一 ID 中隐藏多个 div?
【发布时间】:2010-03-24 16:39:45
【问题描述】:

我正在尝试为 Wordpress cmets 编写一个简短的 Wordpress JQuery,它允许用户打开和关闭特定的 cmets。这是我的第一个剧本,我过得很艰难。

在“comment_options”中,DIV 是一系列控制各个 cmets 的按钮(回复、引用、编辑、关闭等)。关闭按钮是我正在尝试编写此脚本的目的。我需要它来切换“gravtar”和“comment_content”DIV,但保留其余部分,以便它仍然显示用户 ID 和控件。

但是,我似乎不知道如何包含该操作。

[编辑] 这是更新后的代码:

// Custom comments callback
    function steelfrog_comments($comment, $args, $depth) {
        $GLOBALS['comment'] = $comment; ?>

        <li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>" class="comment_comment">

            <div class="comment_info">  

                <div class="gravatar">
                    <?php echo get_avatar($comment,$size='80',$default='<path_to_url>' ); ?>
                </div>

                <div class="comment_poster">
                    <h5><?php comment_author_link(); ?></h5>
                    <span><?php printf(__('%1$s at %2$s'), get_comment_date(),  get_comment_time()) ?></span>
                </div>

                <div class="comment_options">
                    <ul class="options">
                        <li class="reply"><a href="#" title="Reply to this comment"><span>Reply to this comment</span></a></li>
                        <li class="quote"><a href="#" title="Quote and reply this comment"><span>Quote this comment</span></a></li>
                        <li class="link"><a href="#" title="Link to this comment"><span>Link to this comment</span></a></li>
                        <li class="website"><a href="#" title="Website of commentor"><span>Website of commentor</span></a></li>
                        <li class="edit"><a href="#" title="Edit this comment"><span>Edit this comment</span></a></li>
                        <li class="close"><div class="trigger"><a href="#" title="Remove this comment until the page is refreshed"><span>Remove this comment until the page is refreshed</span></a></div></li>
                    </ul>
                </div>
            </div>

            <div class="comment_content">
                <?php comment_text() ?>
            </div>
        <?php } 

以及当前的 JQuery 脚本:

$(document).ready(function(){
    $("div.trigger").click(function() {
        $(this).parent.parent("li").children(".gravatar, .comment_content").slideToggle();
    });
});

[编辑] 通过使用两个单独的选择器让它工作,但它确实很丑。

$(document).ready(function(){

    $(".trigger").click(function(){
        $(this).parent().parent().parent().parent().parent().children(".gravatar, .comment_content").slideToggle('300');
        $(this).parent().parent().parent().parent().children(".gravatar").toggle('300');
        return false
    });

});

【问题讨论】:

    标签: jquery wordpress


    【解决方案1】:

    你想找到与触发元素相关的 div,像这样

    $("div.trigger").click(function() {
      $(this).closest("li:not(.close)")
             .find(".gravatar, .comment_content").slideToggle();
    });
    

    这会爬到包裹.trigger&lt;li&gt;,然后在其中查找那些div。

    你之前的,像这样:$("div.gravatar"),搜索所有&lt;div class="gravatar"&gt;,而不是只在某个元素内,这就是上面的代码将改变的地方。

    【讨论】:

    • 感谢您的回复!我已经编辑了这个例子。触发器在“comment_options”DIV 中找到,所以我假设这不起作用,除非它在子元素和父元素中搜索并返回两个方向上最接近的?
    • @steelfrog - .closest() 找到与该选择器匹配的最近的父元素,因此它上升到&lt;li&gt;,然后下降到您想要在其中的&lt;div&gt; 元素。试试看,应该没问题:)
    • Edit 哦,我想我不能在回复中发布代码。无论如何,我试过了,它似乎不起作用。我不确定为什么。对我来说这听起来很合乎逻辑。
    • @steelfrog - 在.comment_options div 中是否有&lt;li&gt;?如果是这样,您可以使用 html 更新问题吗?
    • 更新了原帖。如果有任何帮助,还放置一个指向实时页面的链接。
    【解决方案2】:

    为什么不直接隐藏完整的 div.trigger 呢? ^^ 好吧,也许这对你不起作用...... 你可以这样做

    $("div.trigger").click(function() {
        $("div.trigger > div").slideToggle();
    });
    

    这将隐藏 div.trigger 下的所有 div

    【讨论】:

    • div 处于触发状态...重新阅读问题 :) 此外,您的答案将隐藏任何触发下的所有 div。
    • 不幸的是,我需要保持显示“gravatar”和“comment_poster”div。此外,触发器 div 是“comment_poster”的子级。抱歉,我应该在发布问题时更清楚地说明这一点。
    猜你喜欢
    • 1970-01-01
    • 2011-02-12
    • 1970-01-01
    • 1970-01-01
    • 2013-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-21
    相关资源
    最近更新 更多