is Javascript able to unload an image from an HTML page?

后端 未结 3 1367
别跟我提以往
别跟我提以往 2021-01-18 06:32

I would like to know if it is possible to unload an image from an HTML page and free its memory occupation with some Javascript instructions. Say an image is already display

3条回答
  •  时光说笑
    2021-01-18 07:05

    The comments on your question are correct, you can remove it from the DOM, but the browser will clear it from memory when it decides it's good and ready.

    To clear it from the DOM, you would do something like this:

    var badImage = document.querySelector("img#idOfImage"); 
    //or "img[href='nameofimagefile.jpeg']" 
    //whatever you need to do to get the right element
    //then, remove it:
    
    badImage.parentElement.removeChild(badImage);
    

提交回复
热议问题