Javascript能够从HTML页面卸载图像吗? [英] is Javascript able to unload an image from an HTML page?

查看:86
本文介绍了Javascript能够从HTML页面卸载图像吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以从HTML页面卸载图像,并通过一些Javascript指令释放其内存占用。假设图像已经显示在屏幕上。说我需要卸载它来节省内存(原因在这里不相关)。我可以用Javascript做到吗?

解决方案

您的问题的评论是正确的,您可以从DOM中移除它,但浏览器会在它确定好并准备好时从内存中清除它。

要从DOM中清除它,您可以这样做:

  var badImage = document.querySelector(img#idOfImage); 
//或img [href ='nameofimagefile.jpeg']
//无论您需要如何获得正确的元素
//然后删除它:

badImage.parentElement.removeChild(badImage);


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 displayed on the screen. Say I need to unload it to save memory (reason is not relevant here). Can I do it with Javascript?

解决方案

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);

这篇关于Javascript能够从HTML页面卸载图像吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆