简体   繁体   English

jQuery if div empty display 2nd div else first

[英]jquery if div empty display 2nd div else first

If #ccs-inline has content then I want to hide #inpage otherwise I want to show #inpage . 如果#ccs-inline有内容,那么我想隐藏#inpage否则我想显示#inpage The issue I have is that if the first div has no content but second div does then it doesn't display the content of second div. 我的问题是,如果第一个div没有内容,但是第二个div却没有显示第二个div的内容。

$(document).ready(function(){
    if ($('#ccs-inline:not(:empty)')){
    $('#inpage').hide();
    } else {
        $('#inpage').show();
    }
});

Use .length , to confirm there is no empty div. 使用.length来确认没有空的div。 Since the jQuery returns a jQuery object hence your if condition will always be true. 由于jQuery返回jQuery对象,因此if条件始终为true。

Example code: 示例代码:

$(document).ready(function() {
  if ($('#ccs-inline:not(:empty)').length > 0) {
    $('#inpage').hide();
  } else {
    $('#inpage').show();
  }
});

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

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