鸳鸯亭资源网 Design By www.gvabc.com
本文实例讲述了JS实现的input选择图片本地预览功能。分享给大家供大家参考,具体如下:
预览效果见下图:
HTML代码如下:
<div class="content" style="margin-top:100px;height:200px;"> <div id="div4bm" style="float:left;"> <!--input[button] 触发 file click事件--> <input type="button" value="选择文件" id="mybutton" class="mybtn" onclick="Id('file').click();" /> <!--file 隐藏起来 触发onchange事件--> <input type="file" name="file" accept="image/png,image/jpg,image/jpeg" id="file" onchange="changeToop();" style="display:none;" /> </div> <!--图片展示区域--> <div style="float:left;"> <!--设置默认图片--> <img id="myimg" src="/UploadFiles/2021-04-02/zanwu.png">CSS代码如下:
.mybtn{ width:100px; height:30px; display:inline-block; background-color:rgb(91,183,91); border:1px solid rgb(91,183,91); border-radius:3px; color:white; font-size:14px; font-family:微软雅黑; cursor:pointer; text-align:center; vertical-align: center; box-shadow:0px 0px 1px 1px rgb(91,160,91); } .mybtn:hover{ background-color:rgb(91,160,91); border-color:rgb(91,160,91); color:white; text-decoration:none; } .myinp{ width:100px; height:30px; display:inline-block; border:1px solid rgb(209,232,250); border-radius:2px; } #div4bm{ padding-top:15px; margin-right:15px; } #mybutton{ margin-left:100px; } #myimg{ width:164px; height:164px; }JS代码如下:
//定义id选择器 function Id(id){ return document.getElementById(id); } function changeToop(){ var file = Id("file"); if(file.value==''){ //设置默认图片 Id("myimg").src='http://sandbox.runjs.cn/uploads/rs/72/huvtowwn/zanwu.png'; }else{ preImg("file","myimg"); } } //获取input[file]图片的url Important function getFileUrl(fileId) { var url; var file = Id(fileId); var agent = navigator.userAgent; if (agent.indexOf("MSIE")>=1) { url = file.value; } else if(agent.indexOf("Firefox")>0) { url = window.URL.createObjectURL(file.files.item(0)); } else if(agent.indexOf("Chrome")>0) { url = window.URL.createObjectURL(file.files.item(0)); } return url; } //读取图片后预览 function preImg(fileId,imgId) { var imgPre =Id(imgId); imgPre.src = getFileUrl(fileId); }上述代码可使用在线HTML/CSS/JavaScript前端代码调试运行工具:http://tools.jb51.net/code/WebCodeRun测试运行效果。
也可使用在线HTML/CSS/JavaScript代码运行工具:http://tools.jb51.net/code/HtmlJsRun运行如下完整代码得到上面图示效果。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>www.jb51.net js input选择图片本地预览</title> <style> .mybtn{ width:100px; height:30px; display:inline-block; background-color:rgb(91,183,91); border:1px solid rgb(91,183,91); border-radius:3px; color:white; font-size:14px; font-family:微软雅黑; cursor:pointer; text-align:center; vertical-align: center; box-shadow:0px 0px 1px 1px rgb(91,160,91); } .mybtn:hover{ background-color:rgb(91,160,91); border-color:rgb(91,160,91); color:white; text-decoration:none; } .myinp{ width:100px; height:30px; display:inline-block; border:1px solid rgb(209,232,250); border-radius:2px; } #div4bm{ padding-top:15px; margin-right:15px; } #mybutton{ margin-left:100px; } #myimg{ width:164px; height:164px; } </style> </head> <body> <div class="content" style="margin-top:100px;height:200px;"> <div id="div4bm" style="float:left;"> <!--input[button] 触发 file click事件--> <input type="button" value="选择文件" id="mybutton" class="mybtn" onclick="Id('file').click();" /> <!--file 隐藏起来 触发onchange事件--> <input type="file" name="file" accept="image/png,image/jpg,image/jpeg" id="file" onchange="changeToop();" style="display:none;" /> </div> <!--图片展示区域--> <div style="float:left;"> <!--设置默认图片--> <img id="myimg" src="/UploadFiles/2021-04-02/zanwu.png">更多关于JavaScript相关内容感兴趣的读者可查看本站专题:《JavaScript图片操作技巧大全》、《JavaScript图形绘制技巧总结》、《JavaScript错误与调试技巧总结》、《JavaScript数据结构与算法技巧总结》、《JavaScript遍历算法与技巧总结》及《JavaScript数学运算用法总结》
希望本文所述对大家JavaScript程序设计有所帮助。
鸳鸯亭资源网 Design By www.gvabc.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
鸳鸯亭资源网 Design By www.gvabc.com
暂无评论...
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新日志
2024年10月03日
2024年10月03日
- 纪钧瀚《钢琴阅读时光 雨中书店聆听轻音乐》[FLAC/分轨][399.62MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[320K/MP3][87.4MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[FLAC/分轨][184.94MB]
- 陈慧娴.2018-Priscilla-Ism演唱会3CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 郑秀文.1999-我应该得到(国)【华纳】【WAV+CUE】
- 陈家慧.2011-钢琴酒吧2CD【龙吟唱片】【WAV+CUE】
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[320K/MP3][45.01MB]
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[FLAC/分轨][109.13MB]
- 赞多《序章》[320K/MP3][45.54MB]
- 许巍.2004-每一刻都是崭新的【步升大风】【WAV+CUE】
- 群星.2024-四方馆影视原声带【韶愔音乐】【FLAC分轨】
- 陈雷.1997-安锁咧【金圆唱片】【WAV+CUE】
- 关淑怡.2013-MY.FAVORITE.SK.3CD【环球】【WAV+CUE】
- Sweety.2006-花言乔语【丰华】【WAV+CUE】
- 李恕权.2003-回·20年全精选2CD【SONY】【WAV+CUE】