惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

TaoSecurity Blog
TaoSecurity Blog
Jina AI
Jina AI
雷峰网
雷峰网
月光博客
月光博客
The GitHub Blog
The GitHub Blog
WordPress大学
WordPress大学
B
Blog RSS Feed
美团技术团队
C
CXSECURITY Database RSS Feed - CXSecurity.com
小众软件
小众软件
Security Latest
Security Latest
Microsoft Azure Blog
Microsoft Azure Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Cybersecurity and Infrastructure Security Agency CISA
Last Week in AI
Last Week in AI
A
Arctic Wolf
Latest news
Latest news
Attack and Defense Labs
Attack and Defense Labs
I
Intezer
F
Fortinet All Blogs
罗磊的独立博客
MongoDB | Blog
MongoDB | Blog
Webroot Blog
Webroot Blog
S
Secure Thoughts
Help Net Security
Help Net Security
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
V
Visual Studio Blog
P
Proofpoint News Feed
博客园 - 【当耐特】
P
Privacy International News Feed
V
Vulnerabilities – Threatpost
Stack Overflow Blog
Stack Overflow Blog
Know Your Adversary
Know Your Adversary
云风的 BLOG
云风的 BLOG
Hacker News: Ask HN
Hacker News: Ask HN
L
LINUX DO - 最新话题
H
Help Net Security
爱范儿
爱范儿
酷 壳 – CoolShell
酷 壳 – CoolShell
S
SegmentFault 最新的问题
Forbes - Security
Forbes - Security
T
Tailwind CSS Blog
量子位
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Tenable Blog
Cloudbric
Cloudbric
N
News and Events Feed by Topic
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Hugging Face - Blog
Hugging Face - Blog

博客园 - 问天何必

网文小说应该怎么从零开始设计故事情节呢? 拒绝花里胡哨,这次来真的,又做了一个导航+超级搜索, 50+功能自定义调整。 一个优秀的导航站。 1个能顶100个 Redis 挂了自动重启的shell 脚本。 宝塔linux面板, 服务器日志分析与流量统计这款插件的mysql版优化。 苹果cms自动采集,重复执行遇到“上次执行时间: --跳过”的解决办法 苹果cms, 后台设置保存不了的解决办法 c# 【电影搜索引擎】采集电影站源码 解决 C:\WINDOWS\system32\inetsrv\rewrite.dll 未能加载。返回的数据为错误. - 问天何必 安利一个聚合搜索导航站,及怎么样设置成默认的搜索引擎 百度站长平台HTTPS认证所遇到的坑 百度Sitemap生成器 女朋友经常问影视剧, 答不上来怎么办? fiddler抓取手机上https数据配置和失败的解决办法 STSDB 一 windows2012 IIS部署GeoTrust证书踩过的坑。 百度编辑器解决span被过滤, 自动加P标签 jquery ajax GET POST 跨域请求实现 代码记录:使用Aforge.net让视频图像反转180度
img error 图片加载失败的最佳方案
问天何必 · 2019-11-20 · via 博客园 - 问天何必

有时候, 当img的src加载失败, 会显示缺省碎片图片,  影响用户体验。  有一个js事件onerror就派上了用场。 它可以在加载失败时, 显示缺省的图片。 

它有两种使用方式。 

第一种: 使用纯标签写法。 这样会增大网页的体积。  但是客户端解析速度要快点。 

<img src="https://www.88tv.org/upload/vod/20190829-1/db8b269c40172799f215aba93f03a03d.jpg" onerror="javascript:this.src='https://t.8kmm.com/upload/vod/20190829-1/db8b269c40172799f215aba93f03a03d.jpg';">

第二种:使用Jquery JS, 全站如果有规律, 可以使用此种方式。  在所需页面插入下面这段js就行了。 

<script>
$(document).ready(function(){  
    $('.stui-vodlist img').each(function(){   //我这里仅仅是遍历vodlist这个元素下面的内容。 
        var error = false;
        if (!this.complete) {
            error = true;
        }

        if (typeof this.naturalWidth != "undefined" && this.naturalWidth == 0) {
            error = true;
        }

        if(error){
            $(this).bind('error.replaceSrc',function(){
                this.src = this.src.replace("www.88tv.org","t.8kmm.com");  //注意这一句,  因为这里是有规律的, 所有我可以这样写,如果有不同, 这里需要定制。 

                $(this).unbind('error.replaceSrc');
            }).trigger('load');
        }
    });
});
</script>

结尾:

如果碰到onerror过去的图片也没有, 可能会造成循环请求, 这样的话, 就需要对这段升级。 

 可以使用, 去检查图片是否存在, 

var imgUrl = "https://t.8kmm.com/upload/vod/20191006-16/d9823993b55e8cb504cf174c7bd9db12.jpg";
        var img = new Image();
        img.src=imgUrl;
        //判断图片大小是否大于0 或者 图片高度与宽度都大于0
        if(img.filesize>0||(img.width>0&&img.height>0)){
            e.src = imgUrl;
        }else{
            //默认图片也不存在的时候
            e.src="/img/default.jpg";
        }

当然, 也可以使用XMLHTTP远程判断图片是否存在。再根据远程获取的是404还是200来更换图片SRC。