简体   繁体   English

如果 img src 使用 javascript 为空,如何删除 div

[英]How to remove div if img src is empty with javascript

Hi I am trying to figure out how to remove a div if the img src is empty.嗨,我想弄清楚如果 img src 为空,如何删除 div。

I've searched on stackoverflow, but most are all jq based.我在stackoverflow上搜索过,但大多数都是基于jq的。 Can someone help in vanilla javascript?有人可以在香草 javascript 中提供帮助吗?

<div class="swiper-wrapper ">
      <div class="swiper-slide">
          <img
             src="https://images.unsplash.com/photo-1526947425960-945c6e72858f?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTN8fHByb2R1Y3RzfGVufDB8fDB8fA%3D%3D&w=1000&q=80"
             alt=""
             class="imgCard"
             color=""
           />
       </div>
       <div class="swiper-slide">
          <img
             src=""
             alt=""
             class="imgCard"
             color=""
           />
       </div>
</div>

Here how you can do this在这里你可以如何做到这一点

 const swipers = document.querySelectorAll('.swiper-slide'); swipers.forEach(e => { const imgSource = e.querySelector('img').getAttribute('src'); if (imgSource.trim() === '') { e.remove() } })
 <div class="swiper-wrapper "> <div class="swiper-slide"> <img src="https://images.unsplash.com/photo-1526947425960-945c6e72858f?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTN8fHByb2R1Y3RzfGVufDB8fDB8fA%3D%3D&w=1000&q=80" alt="" class="imgCard" color="" /> </div> <div class="swiper-slide"> <img src="" alt="" class="imgCard" color="" /> </div> </div>

You can get all images which are child of .swiper-slide class and check if their have src attribute different than an empty string like this您可以获得所有属于.swiper-slide class 的子图像,并检查它们的src属性是否不同于这样的空字符串

 let imgs = document.querySelectorAll(".swiper-slide img"); imgs.forEach(item => { if (item.getAttribute('src') === "") { item.parentNode.remove(); } });
 <div class="swiper-wrapper "> <div class="swiper-slide"> <img src="https://images.unsplash.com/photo-1526947425960-945c6e72858f?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTN8fHByb2R1Y3RzfGVufDB8fDB8fA%3D%3D&w=1000&q=80" alt="First image alternate text" class="imgCard" color="" /> </div> <div class="swiper-slide"> <img src="" alt="Second image alternate text" class="imgCard" color="" /> </div> </div>

 myImgs = document.getElementsByClassName('imgCard'); for (i = 0; i < myImgs.length; i++) { if (myImgs[i].src == '') { myImgs[i].parentNode.style.display = 'none'; } }
 <div class="swiper-wrapper "> <div class="swiper-slide"> <img src="https://images.unsplash.com/photo-1526947425960-945c6e72858f?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTN8fHByb2R1Y3RzfGVufDB8fDB8fA%3D%3D&w=1000&q=80" alt="" class="imgCard" color="" /> </div> <div class="swiper-slide"> <img src="" alt="" class="imgCard" color="" /> </div> </div>

In the above code, we are first getting all the Images using the getElementsByClassName() function在上面的代码中,我们首先使用getElementsByClassName() function 获取所有图像
This function returns an array which we iterate using a for loop这个 function 返回一个数组,我们使用 for 循环对其进行迭代
In the for loop, we hide the parent element (div) by setting its display to none as removing the element will not be recommended and you could still bring it back if you want to.在 for 循环中,我们通过将其显示设置为none来隐藏父元素 (div),因为不建议删除该元素,如果您愿意,仍然可以将其带回。

Hope it solves your problem, Thank You!希望能解决你的问题,谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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