javascript - 有没有办法让图像显示在:none not get downloaded by the browser?内

标签 javascript jquery html css

我希望浏览器(尤其是移动 webkit)不要下载 display:none div 内的图像。现在,它们被下载但未渲染。

有 jquery 插件可以做到这一点吗?

最佳答案

您可以使用data-* 属性。这样,您就可以让 jQuery 按需加载它们:

<img data-source="image_path">

//this one gets all images and loads them
$('img').each(function(){
    //loads the source from data-source
    this.src = this.getAttribute('data-source');
});

<img data-source="image_path" class="foo">
<img data-source="image_path" class="foo">

//this one gets all images that have class foo and loads them
$('img.foo').each(function(){
    //loads the source from data-source
    this.src = this.getAttribute('data-source');
});

当然,您需要将其包装在一个函数中,以便您可以根据需要调用哪些图像。像:

function loadImg(selector){
    $(selector).each(function(){
        this.src = this.getAttribute('data-source');
    });
}

//load images with class foo:
loadImg('.foo');

关于javascript - 有没有办法让图像显示在:none not get downloaded by the browser?内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10003935/

相关文章:

javascript - 固定 Tipsy 工具提示在 Raphaël 路径节点上的位置

javascript - 如何在浏览器中查找javascript发生异常的行

javascript - jquery 窗口滚动事件不能正常工作与 overflow-x 隐藏

html - 如何从图标高度中心垂直居中对齐文本

jquery - 使用 16.66vw 在页面上显示 6 个相同大小的框

javascript - 我可以覆盖/重新定义 "global"Javascript 函数,如 confirm() 和 alert() 吗?

javascript - 如何从字符串中删除数字?

javascript - 如何使用动态页面内容使元素静态化

javascript - 这种 JavaScript 是什么?并帮助处理 AJAX 请求

html - 一页有 2 个不同的 css 文件,浏览器仅应用一个文件中的样式