在开发中,文件上传必不可少但是它长得又丑、浏览的字样不能换,一般会让其隐藏点其他的标签(图片等)来时实现选择文件上传功能

在开发中,文件上传必不可少,<input type="file" /> 是常用的上传标签,但是它长得又丑、浏览的字样不能换,我们一般会用让,<input type="file" />隐藏,点其他的标签(图片等)来时实现选择文件上传功能。 
看代码:

代码如下:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="js/jquery/jquery-1.8.2.min.js" type="text/javascript"></script>
<style type="text/css">
._box
{
width: 119px;
height: 37px;
background-color: #53AD3F;
background-image: url(images/bg.png);
background-repeat: no-repeat;
background-position: 0 0;
background-attachment: scroll;
line-height: 37px;
text-align: center;
color: white;
cursor: pointer;
}
.none
{
width: 0px;
height: 0px;
display: none;
}
</style>
<title>js 实现 input file 文件上传 /></title>
</head>
<body>
<form id="form1" runat="server" method="post" enctype="multipart/form-data">
<div>
<div class="_box">选择图片</div>
</div>
<div class="none">
<input type="file" name="_f" id="_f" />
</div>
</form>
</body>
</html>
<script type="text/javascript">
jQuery(function () {
$("._box").click(function () {
$("#_f").click();
});
});
</script>

但是在火狐和一些高版本的浏览器中后台可以获取到要上传的文件,一些低版本的浏览器压根就获取不到Request.Files 
查阅资料,有说改成这样的:

代码如下:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="js/jquery/jquery-1.8.2.min.js" type="text/javascript"></script>
<style type="text/css">
._box
{
width: 119px;
height: 37px;
background-color: #53AD3F;
background-image: url(images/bg.png);
background-repeat: no-repeat;
background-position: 0 0;
background-attachment: scroll;
line-height: 37px;
text-align: center;
color: white;
cursor: pointer;
}
.none
{
width: 0px;
height: 0px;
display: none;
}
</style>
<title>js 实现 input file 文件上传 /></title>
</head>
<body>
<form id="form1" runat="server" method="post" enctype="multipart/form-data">
<div>
<div class="_box">选择图片</div>
</div>
<div class="none">
<input type="file" name="_f" id="_f" />
</div>
</form>
</body>
</html>
<script type="text/javascript">
jQuery(function () {
$("._box").click(function () {
return $("#_f").click();
});
});
</script>

加了一个return关键字,兼容性提高了不少,但是有的浏览器还是不好用。 
我们发现只有手动点击<input type="file" />后台就一定能获取到要上传的文件 
于是我们可以透明<input type="file" /> 
修改代码如下:

代码如下:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
._box
{
position: relative;
width: 119px;
height: 37px;
background-color: #53AD3F;
background-image: url(images/bg.png);
background-repeat: no-repeat;
background-position: 0 0;
background-attachment: scroll;
line-height: 37px;
text-align: center;
color: white;
cursor: pointer;
overflow: hidden;
z-index: 1;
}
._box input
{
position: absolute;
width: 119px;
height: 40px;
line-height: 40px;
font-size: 23px;
opacity: 0;
filter: "alpha(opacity=0)";
filter: alpha(opacity=0);
-moz-opacity: 0;
left: -5px;
top: -2px;
cursor: pointer;
z-index: 2;
}
</style>
<title>js 实现 input file 文件上传 /></title>
</head>
<body>
<form id="form1" runat="server" method="post" enctype="multipart/form-data">
<div>
<div class="_box">
<input type="file" name="_f" id="_f" />
选择图片
</div>
</div>
</form>
</body>
</html>

我们点击选择图片实际点击了不透明度为0的 <input type="file" />,单用户切看不到 <input type="file" />后台亦可以获取到要上传的文件了。 
ok 
总结: 
用一个不透明度为0的 <input type="file" />盖在要用户可见的标签(或图片等)上,让用户点击。 
用 width height line-height font-size 来控制<input type="file" />右侧浏览按钮的大小。 
用 left top (right 、 bottum)来控制<input type="file" />右侧浏览按钮的位置,可以设置为负值。 
用z-index来设置它们的层覆盖关系。 
form 必须有enctype="multipart/form-data"标记才能上传文件

最新文章

  1. FPGA与simulink联合实时环路系列——实验二LED
  2. TCP学习之二:客户端与服务端的连接
  3. CI加载model的问题
  4. iframe
  5. [U3D 导出Xcode工程包,用Xcode给U3D脚本传递参数]
  6. IOS第11天(4:UIDatePicker时间选择,和键盘处理,加载xib文件,代理模式)
  7. Solr Zookeeper ACL权限配置
  8. ios开发--集成银联3.3.0
  9. 支持IE6的树形节结构TreeTable实际应用案例
  10. 测试使用Windows Live Writer
  11. IntelliJ IDEA 文件夹重命名--解决重命名后js文件引用找不到路径报404错误
  12. Active MQ C#实现
  13. rac各节点实例需设置为相同的一些参数
  14. QoS令牌桶工作原理
  15. java applet初探之计算器
  16. java 基础知识六 字符串1
  17. 【原创】自用css reset
  18. 指数型生成函数 及 多项式求ln
  19. Bootstrap 总结
  20. bootstrap框架下 单选按钮组的选中以及取值问题

热门文章

  1. ABP+AdminLTE+Bootstrap Table权限管理系统第六节--abp控制器扩展及json封装
  2. [转载]无旋treap:从好奇到入门(例题:bzoj3224 普通平衡树)
  3. 使用bootstrap网格系统自适应盒子宽度时保持所有盒子高度一致。
  4. HTML-标签:图片 超链接
  5. 如何在.xml中配置Servlet信息
  6. UnitOfWork知多少
  7. 简单倒计时js代码
  8. c#中的interface abstract与virtual介绍
  9. Oracle 与Mysql区别
  10. 【Spring 核心】AOP 面向切面编程