开发者

How to hide all images using javascript?

I'm injecting JavaScript code into a website for personal use, using a Google Chrome extension... I'd like to hide all im开发者_如何学JAVAages before the page loads... I've got the script to run before anything loads, but can't seem to get the correct code to hide the array of images... something like:

function beforeload(){
  document.getElementsByTagName('img')[0].style.display = "none"
}

Basically i want all the image tags to have style="display:none" added to the attributes. how do i do this?


You need to loop on them

var images = document.getElementsByTagName('img');
for (i = 0; i < images.length;i++ ) {
    images[i].style.display = "none";
}


Amr has got the way to do it with javascript. If you add jquery to the page, it only takes one line

$('img').hide();


Below code will only hide images of all image elements

let images = document.getElementsByTagName("img");
    let images_length = images.length;
    for (let i = 0; i < images_length; i++) {
      images[i].style.setProperty("display", "none", "important");
    }

but what if images are displayed using CSS ? Solution for all elements

 let images = document.getElementsByTagName("img");
    let images_length = images.length;
    for (let i = 0; i < images_length; i++) {
      images[i].style.setProperty("display", "none", "important");
    }

    /** now also hide images which are implemented in css */
    let all_elements = document.querySelectorAll("*");
    for(let i = 0 ; i < all_elements.length ; i++){
      all_elements[i].style.setProperty("background-image","unset","important");
    }
.image {
  width : 100px;
  height : 100px;
  background-image : url(https://image.shutterstock.com/image-photo/aerial-view-main-faisal-mosque-600w-1242735640.jpg);
}
<img src="https://image.shutterstock.com/image-photo/aerial-view-main-faisal-mosque-600w-1242735640.jpg" width="100" height="100">
<div class="image"></div>
<div> To show images plz comment/remove js </div>


Check this out: http://ncthakur.itgo.com/js09.htm

It's not exactly what are you looking for, but you can use some part of it.

It took me 7 seconds to find it on google ;)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜