gpt4 book ai didi

html - 将图像 src 设置为空

转载 作者:技术小花猫 更新时间:2023-10-29 12:19:07 25 4
gpt4 key购买 nike

我想将图像设置为空。通过执行 src="" 许多人说它可能会给浏览器带来问题:

http://www.nczonline.net/blog/2009/11/30/empty-image-src-can-destroy-your-site/

所以我不确定下面是否会遇到与将 src 设置为空相同的问题:

<img id="myImage">

因为在这种情况下没有 src 属性。

因此,如果我想一开始就将图像设置为空,我能做的最好的事情是什么?

最佳答案

最佳解决方案

初始化图像如下:src="//:0"喜欢here : <img id="myImage" src="//:0">

Edit: as per the comment of Alex below, using //:0 apparently can trigger the onError event of the img. So beware of this.

Edit 2: From comment by Drkawashima: As of Chrome 61 src="//:0" no longer seems to trigger the onError event.


其他解决方案

或者,您可以使用非常小的图像,直到您真正填满 src。标签:like this image .使用这个“非常小的图像”,您可以像这样初始化标签:

<img src="data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs%3D" width="0" height="0" alt="" />

source


从 DOM 中移除元素

或者,如果您只是不想让该元素出现在 DOM 中,只需使用一些 JavaScript:

var myObject = document.getElementById('myObject');
myObject.parentNode.removeChild(myObject);

(根据 this answer ,不推荐使用 JavaScript 的 .remove() 函数,因为浏览器对 DOM 4 的支持很差)

或者只使用一些 jQuery:

$("#myObject").remove();

关于html - 将图像 src 设置为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19126185/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com