简体   繁体   中英

Remove Parent element child elements href has no property

I was trying to remove the parent element if the child element href has no attribute.

 $(document).ready(function() { var noImg = ""; var cPathName = window.location.pathname; if (noImg) { $(this).parent().remove(); } }); $(".dnext-thumbs-gallery-top-image-link").filter(function() { if ($(this).attr('href').length == 0) { console.log('test'); $(this).parent().parent().remove(); } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="cs-gallery" class="et_pb_with_border et_pb_module dnxte_thumbs_gallery_parent dnxte_thumbs_gallery_parent_0_tb_body"> <div class="et_pb_module_inner"> <div class="dnext_thumbs_gallery_top_holder"> <div class="swiper-container dnext-thumbs-gallery-top swiper-container-initialized swiper-container-horizontal"> <div class="swiper-wrapper dnext-thumbs-gallery-active" data-lightbox="on"> <div class="et_pb_module dnxte_thumbs_gallery_child dnxte_thumbs_gallery_child_0_tb_body swiper-slide"> <div class="et_pb_module_inner dnext-thumbs-gallery-item"> <a href="" class="dnext-thumbs-gallery-top-image-link" data-title="Logo Item"><img class="img-fluid" alt="Logo Item" src=""></a> </div> </div> <div class="et_pb_module dnxte_thumbs_gallery_child dnxte_thumbs_gallery_child_1_tb_body swiper-slide swiper-slide-prev" style="width: 1080px; margin-right: 10px;"> <div class="et_pb_module_inner dnext-thumbs-gallery-item"> <a href="https://adroithd.com/wp-content/uploads/2022/07/WhatsApp-Image-2022-03-28-at-11.45.05-AM.jpeg" class="dnext-thumbs-gallery-top-image-link" data-title="Logo Item"><img class="img-fluid" alt="Logo Item" src="https://adroithd.com/wp-content/uploads/2022/07/WhatsApp-Image-2022-03-28-at-11.45.05-AM.jpeg.webp"></a> </div> </div> </div> </div> </div> </div> </div>

when I trying to filter. got an error

$(...).filter is not a function

how to solve this error?

You can use each instead of filter.

 $(document).ready(function() { var noImg = ""; var cPathName = window.location.pathname; if (noImg) { $(this).parent().remove(); } }); $(".dnext-thumbs-gallery-top-image-link").each(function(i, e) { if ($(e).attr('href').length == 0) { $(this).parent().parent().remove(); } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="cs-gallery" class="et_pb_with_border et_pb_module dnxte_thumbs_gallery_parent dnxte_thumbs_gallery_parent_0_tb_body"> <div class="et_pb_module_inner"> <div class="dnext_thumbs_gallery_top_holder"> <div class="swiper-container dnext-thumbs-gallery-top swiper-container-initialized swiper-container-horizontal"> <div class="swiper-wrapper dnext-thumbs-gallery-active" data-lightbox="on"> <div class="et_pb_module dnxte_thumbs_gallery_child dnxte_thumbs_gallery_child_0_tb_body swiper-slide"> <div class="et_pb_module_inner dnext-thumbs-gallery-item"> <a href="" class="dnext-thumbs-gallery-top-image-link" data-title="Logo Item"><img class="img-fluid" alt="Logo Item" src=""></a> </div> </div> <div class="et_pb_module dnxte_thumbs_gallery_child dnxte_thumbs_gallery_child_1_tb_body swiper-slide swiper-slide-prev" style="width: 1080px; margin-right: 10px;"> <div class="et_pb_module_inner dnext-thumbs-gallery-item"> <a href="https://adroithd.com/wp-content/uploads/2022/07/WhatsApp-Image-2022-03-28-at-11.45.05-AM.jpeg" class="dnext-thumbs-gallery-top-image-link" data-title="Logo Item"><img class="img-fluid" alt="Logo Item" src="https://adroithd.com/wp-content/uploads/2022/07/WhatsApp-Image-2022-03-28-at-11.45.05-AM.jpeg.webp"></a> </div> </div> </div> </div> </div> </div> </div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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