简体   繁体   English

如何使用jQuery在父div中隐藏任何子元素

[英]How to hide any child elements inside a parent div using jquery

I have two cases, One when there is comments and one when there is not. 我有两种情况,一种是有评论,另一种是没有评论。 I have separated two cases with php like this: 我已经用php分隔了两种情况:

    <div class="box">
        <textarea required id="reviews_comment" name="comment" value="<?php echo $this->input->post('comment'); ?>" ></textarea>
        <button style="display: none;" id="comment_button" class="btn btn-success"  >Add Comment</button>
        <div id="comment_div">
        <b>Comments...</b></br>
        <?php if($comment_data){ foreach ($comment_data as $data) {?>
        <b>By: <span><?php echo $data->first_name.' '.$data->last_name; ?></span></b>
        <p><?php echo $data->comment; ?></p>
        <?php } } else{?>
        <div id="no_comment_case" ><h1>No comments yet...</h1></div>
        <?php } ?>
    </div>

Now when there is no comment No comment case will be displayed. 现在,当没有评论时,将不显示评论大小写。 After user posts a comment with ajax i have tried the following code to hide the no comment case and only append the recent comment, but the no comment case is not being hidden. 用户使用ajax发表评论后,我尝试使用以下代码隐藏“无评论”案例,仅追加最近的评论,但“无评论”案例未被隐藏。 The jquery code is: jQuery代码是:

      success: function(msg){
                console.log(msg);
                if(msg=="success")
                {
                   $('#comment_button').hide();
                   $('#reviews_comment',$('#comment_div')).hide();
                   // $('div#comment_div> div:eq(2)').css("display", "none");
                   html='<b>By: <span>'+username+'</span></b><p>'+review+'</p>';
                   $('#comment_div').append(html);

                }

              }

How can I do it. 我该怎么做。 Any kind of suggestion are highly appreciated. 任何建议都将受到高度赞赏。 Thanks. 谢谢。

Enclose comment_div properly,

<div class="box">
            <textarea required id="reviews_comment" name="comment" value="<?php echo $this->input->post('comment'); ?>" ></textarea>
            <button style="display: none;" id="comment_button" class="btn btn-success"  >Add Comment</button>
            <div id="comment_div">
            <b>Comments...</b></br>

            <?php if($comment_data){ foreach ($comment_data as $data) {?>
                <b>By: <span><?php echo $data->first_name.' '.$data->last_name; ?></span></b>
                <p><?php echo $data->comment; ?></p>
            <?php } 
            } else{?>
                <div id="no_comment_case" ><h1>No comments yet...</h1></div>
            <?php } ?>

            </div>
        </div>

    Ajax call:

    success: function(msg){
                    if(msg=="success")
                    {
                       $('#comment_button').hide();
            $('#reviews_comment,#comment_div,#no_comment_case').hide();
                       html='<b>By: <span>'+username+'</span></b><p>'+review+'</p>';
                       $('#comment_div').append(html);

                    }

                  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM