<div class="codetitle"> <span><a style="CURSOR: pointer" data="14597" class="copybut" id="copybut14597" onclick="doCopy('code14597')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code14597"> <br> <br> <br> <br><meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <br><script type="text/javascript"> <BR>/** <BR>* 从 file 域获取 本地图片 url <BR>*/ <BR>function getFileUrl(sourceId) { <BR>var url; <BR>if (navigator.userAgent.indexOf("MSIE")>=1) { // IE <BR>url = document.getElementById(sourceId).value; <BR>} else if(navigator.userAgent.indexOf("Firefox")>0) { // Firefox <BR>url = window.URL.createObjectURL(document.getElementById(sourceId).files.item(0)); <BR>} else if(navigator.userAgent.indexOf("Chrome")>0) { // Chrome <BR>url = window.URL.createObjectURL(document.getElementById(sourceId).files.item(0)); <BR>} <BR>return url; <BR>} <br><br>/** <BR>* 将本地图片 显示到浏览器上 <BR>*/ <BR>function preImg(sourceId, targetId) { <BR>var url = getFileUrl(sourceId); <BR>var imgPre = document.getElementById(targetId); <BR>imgPre.src = url; <BR>} <BR></script> <br> <br> <br><form action=""> <br><input type="file" name="imgOne" id="imgOne" onchange="preImg(this.id,'imgPre');"> <br><img id="imgPre" src="" style="max-width:90%" height="300px" style="display: block;" alt="JS预览图像将本地图片显示到浏览器上_javascript技巧" > <br> </form> <br> <br> <br> </div>