3 回答
TA贡献1829条经验 获得超9个赞
您可以通过使用:empty伪类单独使用 CSS 来实现此效果:
.wrapperDIV h3:empty {
display: none;
}
延伸阅读:
https://developer.mozilla.org/en-US/docs/Web/CSS/:empty
TA贡献1811条经验 获得超5个赞
尝试使用获取文本而不是值
$('.wrapperDIV').each(function (i) {
if( $(this).find('h3').text() === "" ) {
$(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>
TA贡献1770条经验 获得超3个赞
你可以在 vanilla JavaScript 中尝试这个可行的代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>test1</title>
<script>
window.onload = function(){
let div = document.getElementsByClassName('wrapperDIV');
let ch = div[0].children;
for(let i=0;i<ch.length; i++){
if ( ch[i].tagName=='H3' ){
let text = ch[i].textContent;
if (!text) console.log('It\'s h3 without text')
}
}
}
</script>
</head>
<body>
<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>
</body>
</html>
在 jQuery 中,您可以使用如下代码:
$(document).ready(function(){
let h3 = $(".wrapperDIV > h3");
if ( !h3.textContent ) console.log('h3 has no text contents');
});
添加回答
举报