js上传图片及预览功能实例分析
本文实例讲述了js上传图片及预览功能。分享给大家供大家参考。具体分析如下:
参考了网上一些人代码写了一个上传图片及时预览的功能
<imgid="imgTag"style="height:100px;"alt=""/>
<inputtype="file"/>
functionDisplayImage(fileTag,imgTagId){
varallowExtention=".jpg.png.gif";
varextentionArr=fileTag.value.split('.');
varextention=extentionArr[extentionArr.length-1];
if(!(allowExtention.indexOf(extention)>-1)){
alert("Pleaseuploadimage!");
}else{
//foradvecedbroswer(thenewestie,chrome,ff)
if(typeof(FileReader)!=="undefined"){
varreader=newFileReader();
reader.readAsDataURL(fileTag.files[0]);
reader.onload=function(e){
document.getElementById(imgTagId).setAttribute("src",e.target.result);
}
}else{
//for(ie6)
document.getElementById(imgTagId).setAttribute("src",fileTag.value);
}
}
}
希望本文所述对大家的javascript程序设计有所帮助。
热门推荐
10 新年门口花束祝福语简短
11 盘锦结婚祝福语大全简短
12 父母生日祝福语 简短独特
13 家庭恩爱祝福语简短英文
14 高考俄语祝福语大全简短
15 虎年祝福语 诗句唯美简短
16 生日婚礼祝福语简短精辟
17 虎年喝酒拜年祝福语简短
18 教师闺蜜祝福语简短