首页 > 解决方案 > 找到子 H3 并检查是否有值

问题描述

我希望用来jQuery查看一堆<div>具有相同类名的元素,并检查h3这些<div>元素中是否有任何内容。我在想这会起作用(如下所示),但它不是......我做错了什么?

$('.wrapperDIV').each(function (i) {
    if( $(this).closest('h3').val().length == 0 ) {
        $(this).addClass('.hideME');
    }
});

标记是:

<div class="wrapperDIV">
  <span class="ItemNo">5.</span>
     <a href="#" title="Read more about "><img src="" title="" alt=""></a>
        <h3></h3>
        <div class="composer-button">
        <a href="#" class="read-more" title="Read more about ">Read more</a>
    </div>
</div>

标签: javascriptjqueryhtml

解决方案


:empty您可以通过使用伪类单独使用 CSS 来实现此效果:

.wrapperDIV h3:empty {
  display: none;
}

延伸阅读:

https://developer.mozilla.org/en-US/docs/Web/CSS/:empty


推荐阅读