检查是否存在div类来隐藏另一个div

时间:2014-09-10 22:04:42

标签: jquery toggle show-hide

我试图让我的div隐藏,如果存在另一个div,并且在网上查看示例后,我不确定我在这方面做错了什么。任何帮助是极大的赞赏。谢谢!

$('#required-field').toggle(!$('#gform_confirmation_message_1').length);

HTML:

<div id="gforms_confirmation_message" class="gform_confirmation_message_1">
  <meta charset="UTF-8">
  <div id="gform_confirmation_wrapper_1" class="gform_confirmation_wrapper ">
    <div id="gform_confirmation_message_1" class="gform_confirmation_message_1 gform_confirmation_message">
      Thank you for contacting us. We will make every effort to respond to you inquiries shortly.
    </div>
  </div>
</div>

<div id="required-field">* Required Fields</div>

1 个答案:

答案 0 :(得分:0)

使用length检查div是否存在。

$( document ).ready(function() {
    if($("#gform_confirmation_message_1").length > 0){ 
        $("#required-field").hide();
    }
});

如果存在#gform_confirmation_message_1,则隐藏#required-field

<强> JS Fiddle Demo