代码描述:IE浏览器下 js无法获取 隐藏图片尺寸。IE浏览器下js无法获取隐藏图片尺寸简单介绍
当然并不是在所有的IE浏览器会有此现象,IE11中此问题已经被消除。 这里所说的隐藏的图片指的的是采用display:none实现的隐藏效果。 代码实例如下:
<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="https://www.51qianduan.com/" /> <title>51前端</title> <script type="text/javascript"> window.onload=function(){ var img=document.getElementById("img"); var odiv=document.getElementById("show"); odiv.innerHTML=img.width; } </script> </head> <body> <div id="show"></div> <img id="img" src="demo/js/img/antzone.jpg" style="display:none"> </body> </html>
上面的代码在IE11以下浏览器中,得到的数据时0,而在其他浏览器中能够正确获取尺寸。 当然正确获取图像尺寸的方式有很多种,下面分享其中的一种,代码实例如下:
<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="https://www.51qianduan.com/" /> <title>51前端</title> <script type="text/javascript"> window.onload=function(){ var odiv=document.getElementById("show"); var img=document.getElementById("img"); img.style.display="block"; img.style.position="absolute"; img.style.left="-10000px"; img.style.zIndex="-1000"; odiv.innerHTML=img.width; } </script> </head> <body> <div id="show"></div> <img id="img" src="/demo/js/img/antzone.jpg" style="display:none"> </body> </html>
请绑定手机号,在继续操作
注意:只有绑定手机以后,才可使用网站全部功能