[英]How to Remove a parent of an empty tag jquery
I have multiple data which i inserted into an html tag.我有多个数据插入到 html 标签中。 Some of the tags are empty while some are filled with details.有些标签是空的,有些则填满了详细信息。 What i want is how do i remove the parent of the ones that are empty, this is my code我想要的是如何删除空的父级,这是我的代码
HTML HTML
<div class="row order-info">
<div class="col-12">
<h6 class="nameDiv">Hello this is nice</h6>
</div>
<div class="col-12">
<h6 class="proId"></h6>
</div>
<div class="col-12">
<h6 class="proP">testing. testing</h6>
</div>
</div>
JQUERY JQUERY
removeEmpty();
function removeEmpty() {
if($(".order-info div h6").text().length < 1) {
$(this).closest(".col-12").remove();
}
}
Please What am i doing wrong请问我做错了什么
$('.order-info .col-12').has('h6:empty').remove();
This code will remove those elements of class .col-12
that contain an empty h6
element.此代码将删除 class .col-12
中包含空h6
元素的那些元素。
You're not iterating using the css selector.您没有使用 css 选择器进行迭代。
Try this function:试试这个 function:
function removeEmpty() {
$(".order-info div h6").each(function(elem){
if($(this).html().length < 1) {
$(this).closest(".col-12").remove();
}
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.