关于利用input的file属性在页面添加图片的问题
2024-09-01 00:09:50
在页面添加图片涉及到兼容的问题怎么解决兼容问题呢?请看下面分析:
在IE浏览器上面我们能直接通过获取其input的value值来获取其图片的路径。
在火狐和谷歌需要用createObjectURL((input的元素).files.item(0))来获取其路径;
现在请看代码:
css设置样式部分(可以自己设置好看的样式):
*{
margin:;
padding:;
}
#img{
width:50px;
}
.box{
width: 100px;
margin:20px auto;
}
.box span{
width:50px;
height:50px;
display: block;
border-radius:50%;
overflow:hidden;
margin:auto;
}
.box span img{
width: 100%;
}
.Infor_file{
position: relative;
margin-top:20px;
}
.Infor_file p{
width:100px;
height: 40px;
text-align: center;
line-height:40px;
background:#E77B77;
color:#fff;
font-size:16px;
border-radius:5px;
}
#file{
width:100px;
height:40px;
position: absolute;
top:;
left:;
opacity:;
filter:alpha(opacity=0);
}
html部分:
<div class="box">
<span><img id="img" src=""/></span>
<div class="Infor_file">
<p>添加图片</p>
<input type="file" id="file" onchange="get(this)"/>
</div>
</div>
js部分:
var img=document.getElementById("img")
function get(node){
var userAgent=navigator.userAgent;
//适用谷歌和火狐的浏览器传入路径
if(userAgent.indexOf("Chrome")!=-1||userAgent.indexOf("Firefox")!=-1){
img.src=window.URL.createObjectURL(node.files.item(0));
}
//适用IE浏览器传入路径
if(userAgent.indexOf("MSIE")!=-1){
img.src=node.value;
}
}
以上代码仅供参考!
最新文章
- C语言学习 第九次作业总结
- U-BOOT-Linux启动指令bootm分析
- OD常用断点
- iOS的Mantle实战分析
- js动态添加table 数据tr td
- Linux Kernel 2.6.28 以上有BUG,系统运行第208.5天down机
- hash在Coreseek 中配置bigint
- 关于html中利用jQuery选择子节点方法总结——待续
- 【NO.2】Jmeter-安装Jmeter - 在Linux环境安装Jmeter - 在Windows环境安装Jmeter
- ⑤bootstrap表格使用基础案例
- mysql全日志添加时间戳以及SQL多行问题处理(更新)
- 通过url获取相应的location信息
- SQL Server使用侦听器IP访问时遇到";The target principal name is incorrect. Cannot generate SSPI context";
- 【题解】洛谷 P1014 【Cantor表】
- Codeforces Gym100187C Very Spacious Office 贪心 堆
- 我设计的电脑usb红外遥控键盘原理图
- eclipse各版本及下载
- JVM总括四-类加载过程、双亲委派模型、对象实例化过程
- Codeforces 1132C - Painting the Fence - [前缀和优化]
- seaweedFS