- 作者:xiaoxiao
- 发表时间:2020-12-23 10:40
- 来源:未知
很早前收集的TIPS,最近在项目中用到了,可能会对大家有所帮助。
<script>var img=null;function s(){if(img)img.removeNode(true);img=document.createElement("img");img.style.position="absolute";img.style.visibility="hidden";img.width = 0;//如果不要,就可以得到img的大小,但有可能改变原网页的Client区大小,出现滚动条img.height = 0;//同上img.attachEvent("onreadystatechange",orsc);img.attachEvent("onerror",oe);document.body.insertAdjacentElement("beforeend",img);img.src=inp.value;}function oe(){alert("cant load img");}function orsc(){if(img.readyState!="complete")return false;alert("图片大小:"+img.offsetWidth+"X"+img.offsetHeight);alert("图片尺寸:"+img.fileSize);btn.disabled=false;}</script>
<script> var img=null; function s() { if(img)img.removeNode(true); img=document.createElement("img"); img.style.position="absolute"; img.style.visibility="hidden"; img.width = 0; img.height = 0; img.attachEvent("onreadystatechange",orsc); img.attachEvent("onerror",oe); document.body.insertAdjacentElement("beforeend",img); img.src=inp.value; } function oe() { alert("cant load img"); } function orsc() { if(img.readyState!="complete")return false; alert("图片大小:"+img.offsetWidth+"X"+img.offsetHeight); alert("图片尺寸:"+img.fileSize); btn.disabled=false; } </script><!--摘自51js-->
<script>var img=null;function s(){if(img)img.removeNode(true);img=document.createElement("img");img.style.position="absolute";img.style.visibility="hidden";img.width = 0;img.height = 0;img.attachEvent("onreadystatechange",orsc);img.attachEvent("onerror",oe);document.body.insertAdjacentElement("beforeend",img);img.src=inp.value;}function oe(){alert("cant load img");}function orsc(){if(img.readyState!="complete")return false;alert("图片大小:"+img.offsetWidth+"X"+img.offsetHeight);alert("图片尺寸:"+img.fileSize);btn.disabled=false;}</script><input id=inp type="file"><br><button οnclick="s()">Test</button><button id=btn disabled>UpLoad</button> 大家看到,其实也很简单,它先是在内存中定义了一个IMG对象,然后用此对象在客户端得到IMG对象的各种属性。