在具有类的元素中隐藏元素

Hide an element inside an element with a class

本文关键字:元素 隐藏      更新时间:2023-09-26

我想把<p class="crfp-field">隐藏在<ol class="comment-list">里面,如果它不在里面,就把它显示出来。希望你能帮我做这个把戏。以下是代码的样子

<ol class="comment-list">
    <li class="comment">
        <div id="comment-n">
            <p>This is a comment</p>
        </div>
        <ul class="children">
            <li class="comment">
                <div id="comment-n">
                    <p>This is a reply to a comment. Star rating is visible here. I don’t want to display it when I am replying to a comment.</p>
                </div>
                <div id="respond" class="comment-respond">
                    <h3 id="reply-title" class="comment-reply-title">Write a Reply or Comment <small><a class="btn" rel="nofollow" id="cancel-comment-reply-link" href="" style="">Cancel Reply</a></small></h3>
                    <form action="http://../wp-comments-post.php" method="post" id="commentform" class="comment-form">
                        <p class="comment-notes">Your email address will not be published.</p>
                        <p class="comment-form-author">Name<input id="author" name="author" type="text"></p>
                        <p class="comment-form-email">Email<input id="email" name="email" type="text"></p>
                        <p class="comment-form-url">Website<input id="url" name="url" type="text"></p>
                        <!-- CRFP Fields: Start -->
                        <p class="crfp-field">
                            <!-- CRFP Stuff -->
                        </p>
                        <!-- CRFP Fields: End -->
                        <p class="comment-form-comment">Comment<textarea id="comment" name="comment"></textarea></p>
                        <input id="submit-new" value="Post Comment" type="submit">
                    </form>
                </div>
            </li><!-- #comment-## -->
        </ul><!-- .children -->
    </li><!-- #comment-## -->
</ol>

您可以使用CSS,不需要JS:

ol.comment-list p.crfp-field {
    display: none;
}

如果p.crfp-field元素不在ol内部,则将不应用上述规则,并且该元素将正常显示。

这应该做到:

$('.comment-list .crfp-field').hide();

您可以通过以下方式实现:

$(document).ready(function(){
    $("p.crfp-field").show(); //show all p tag with class crfp-field
    $("ol.comment-list p.crfp-field").hide(); //hide the needed ones
});