Jquery检查div是否为空省略:before或:after

时间:2017-08-23 18:23:17

标签: jquery html css css3

我需要检查Jquery是否Div是空的省略:before或:after,使用具有:before或:after的div执行此操作时考虑:before或:after



$(function(){
  if($('#content').not(':empty')) {
    console.log('Yes content');
  } else {    
    console.log('No content');
  }
});

#content:empty {
    position: relative;
    display: block;
    min-height: 200px;
    background-color: rgb(240,240,240);
    text-align: center;
}

#content:empty:before {
    content: 'NO IMAGE';
    position: absolute;
    font-size: 25px;
    left: 0;
    right: 0;
    top: 50%;
    transform: translateY(-50%);
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="content"></div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

您可以使用以下方法检查HTML或文本:

&#13;
&#13;
$(function(){
  if( $('#content').html() || $('#content').text() ){
    console.log('Yes content');
  } else {    
    console.log('No content');
  }
});
&#13;
#content:empty {
    position: relative;
    display: block;
    min-height: 200px;
    background-color: rgb(240,240,240);
    text-align: center;
}

#content:empty:before {
    content: 'NO IMAGE';
    position: absolute;
    font-size: 25px;
    left: 0;
    right: 0;
    top: 50%;
    transform: translateY(-50%);
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="content"></div>
&#13;
&#13;
&#13;

示例:

空:

<div id="content"></div>

带文字:

<div id="content">Hello World</div>

使用HTML:

<div id="content">
    <span class="child"></span>
</div>