修改 input file 的按钮值
在网页中修改头像功能时需要把文件框给隐藏掉,网上找到如下方法解决,兼容firefox和IE。图片选择后检查图片类型是否合法。<style>.fileinput{ width:0px; font-size:12px; margin-left:-50px; margin-top:-3px; position:absolute; filter:alpha(opacity=0); -moz-opacity: 0; opacity: 0;}</style><script>//以下两个方法预览图片var picPath;function loadImage(ele) { checkExt(ele); picPath = getPath(ele); document.getElementById("img"+ele.id).src=picPath;}function getPath(obj){if(obj){//ieif (window.navigator.userAgent.indexOf("MSIE")>=1){obj.select();// IE下取得图片的本地路径return document.selection.createRange().text;}//firefoxelse if(window.navigator.userAgent.indexOf("Firefox")>=1){if(obj.files){// Firefox下取得的是图片的数据return obj.files.item(0).getAsDataURL();}return obj.value;}return obj.value;}}//检查文件类型function checkExt(obj){var errMsg="";var FileExt="";var AllowExt=".jpg|.jpeg|.gif|.bmp|.png|"//全部图片格式类型//var AllowExt=".jpg|.gif|.doc|.txt|";//允许上传的文件类型if(obj.value=="") return false;FileExt=obj.value.substr(obj.value.lastIndexOf(".")).toLowerCase();if(AllowExt!=0&&AllowExt.indexOf(FileExt+"|")==-1) { //判断文件类型是否允许上传 errMsg="\n该文件类型不允许上传。请上传 "+AllowExt+" 类型的文件,当前文件类型为"+FileExt; alert(errMsg); return false;}}</script><input type="file" id="headId" name="head" class="fileinput" onchange="loadImage(this);" title="点击上传个人图片"/><a href="#" >点击上传个人图片</a></p>
页:
[1]