在页面添加图片涉及到兼容的问题怎么解决兼容问题呢?请看下面分析:

在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;
}
}

以上代码仅供参考!

最新文章

  1. C语言学习 第九次作业总结
  2. U-BOOT-Linux启动指令bootm分析
  3. OD常用断点
  4. iOS的Mantle实战分析
  5. js动态添加table 数据tr td
  6. Linux Kernel 2.6.28 以上有BUG,系统运行第208.5天down机
  7. hash在Coreseek 中配置bigint
  8. 关于html中利用jQuery选择子节点方法总结——待续
  9. 【NO.2】Jmeter-安装Jmeter - 在Linux环境安装Jmeter - 在Windows环境安装Jmeter
  10. ⑤bootstrap表格使用基础案例
  11. mysql全日志添加时间戳以及SQL多行问题处理(更新)
  12. 通过url获取相应的location信息
  13. SQL Server使用侦听器IP访问时遇到&quot;The target principal name is incorrect. Cannot generate SSPI context&quot;
  14. 【题解】洛谷 P1014 【Cantor表】
  15. Codeforces Gym100187C Very Spacious Office 贪心 堆
  16. 我设计的电脑usb红外遥控键盘原理图
  17. eclipse各版本及下载
  18. JVM总括四-类加载过程、双亲委派模型、对象实例化过程
  19. Codeforces 1132C - Painting the Fence - [前缀和优化]
  20. seaweedFS

热门文章

  1. Eclipse 注释
  2. 如何测试LBS功能
  3. PayPal为什么从Java迁移到Node.js
  4. 无限“递归”的python程序
  5. C# 6 与 .NET Core 1.0 高级编程 - C# 6 改进
  6. [工作总结] QA小鸟一年了
  7. sql增删查改
  8. iOS 启动页后广告Demo
  9. 转: 尽己力,无愧于心 FastReport.Net 常用功能总汇
  10. matlab中hold指令、figure指令及subplot指令的使用