
当然,文件头的特征符合并不一定说明文件内容就对。
<div><select name="imagesList" id="imagesList">
<option value="">请选择图片...</option>
<option value="https://gss0.baidu.com/-fo3dSag_xI4khGko9WTAnF6hhy/image/h%3D300/sign=c0524600ba003af352bada60052bc619/b58f8c5494eef01fcca8beccecfe9925bc317d7f.jpg">我是能显示的图片1</option>
<option value="https://gss0.baidu.com/-Po3dSag_xI4khGko9WTAnF6hhy/image/h%3D300/sign=615ad02161224f4a4899751339f69044/b3b7d0a20cf431ad184ee53e4736acaf2edd981d.jpg">我是能显示的图片2</option>
<option value="1dsakjodksjakjxx.xxx">我是不能显示的图片</option>
</select>
</div>
<div>
<img id='a' src='https://gss0.bdstatic.com/7Ls0a8Sm1A5BphGlnYG/sys/portrait/item/28ced0c7b3bd6871a81c.jpg?20180330052556' /> 变袭清换图片昌禅毁
</div>
<div>
<img id='b' src='https://gss0.bdstatic.com/70cFsj3f_gcX8t7mm9GUKT-xh_/avatar/100/r6s1g8.gif' />
默认图片
</div>
<script>
window.addEventListener('DOMContentLoaded', function() {
var a = document.getElementById('a')
var b = document.getElementById('b')
var images = document.getElementById('imagesList')
images.onchange = function(e) {
var index = e.target.options.selectedIndex
var value = e.target.options[index].value
if (!value) return
a.src = value
}
a.onerror = 耐备function(e) { // a图片载入失败时候处理,例如一张不存在的图片
var confirm = window.confirm('点击确定将会显示默认图片,取消则会返回')
if (confirm) {
a.src = b.src // document.getElementById('b').src的地址传给a.src
} else {
alert(e.type)
}
}
})
</script>
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)