可以通过让默认的input type = 'file'按钮透明度变为0,并且让它刚好覆盖在自定义的按钮上,来实现此效果:

将它写成一个jQuery插件:

(function($){
$.fn.browseElement = function(){
var input = $("<input type='file' multiple>"); input.css({
"position": "absolute",
"z-index": 2,
"cursor": "pointer",
"-moz-opacity": "0",
"filter": "alpha(opacity: 0)",
"opacity": "0"
}); input.mouseout(function(){
input.detach();
}); $(this).mouseover(function(){
input.css($(this).offset());
input.width($(this).outerWidth());
input.height($(this).outerHeight());
$("body").append(input);
}); return input;
};
})(jQuery);

注意: 使用input.offset($(this).offset),会产生bug,常常出现两倍的左侧距离,还是使用.css方法

然后在页面中自定义一个上传按钮:

#attach {
width:100px;
height:30px;
border:1px solid #00B7FF;
background:#cae2fd;
border-radius:4px;
color:#00B7FF;
font-size:12px;
line-height:30px;
text-align:center;
margin:auto;
padding:0
}
<div id="attach">选择文件</div>

然后对该按钮调用扩展的browseElement方法:

var hiddenInput = $('#attach').browseElement();

hiddenInput.change(function(){
//上传文件时相关处理代码
});

完整代码在:

http://pan.baidu.com/s/1mgwQpew

使用自定义的按钮替换默认的<input type='file'>的更多相关文章

  1. 自定义样式 实现文件控件input[type='file']

    一般我们设计的上传按钮都是和整个页面风格相似的样式,不会使用html原生态的上传按钮,但是怎么既自定义自己的样式,又能使用file控件功能呢? 思路是这样的: 1.定义一个相对定位的DIV,按照整成步 ...

  2. 原生HTML5 input type=file按钮UI自定义

    原生<input type="file" name="file" />长得太丑 提升一下颜值 实现方案一.设置input[type=file]透明度 ...

  3. input type="file"在各个浏览器下的默认样式,以及修改自定义样式

    一.<input type="file"/>在各个浏览器中的默认样式: 系统 浏览器 样式效果 点击效果 mac google 点击按钮和输入框都可以打开文件夹 mac ...

  4. 自定义上传按钮 <input type="file" name = "file"/> (将file隐藏在button下)

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  5. 关于去除input type='file'改变组件的默认样式换成自己需要的样式的解决方案

    在工作中时常会遇到如需要上传功能的按钮,而不像需要系统默认的样式时候,可以采取以下的解决方案: <img onclick="getElementById('file').click() ...

  6. html中,文件上传时使用的<input type="file">的样式自定义

    Web页面中,在需要上传文件时基本都会用到<input type="file">元素,它的默认样式: chrome下: IE下: 不管是上面哪种,样式都比较简单,和很多 ...

  7. input[type='file']默认样式

    <input type="file" name="" id="" value="" /> 当input的ty ...

  8. input type= file 如何更改自定义的样式

    input { @include wh(24px,22px);//sass 宽高 @include pa(0,0); //绝对定位 top:0:left:0: opacity: 0; //透明度: o ...

  9. 修改input type=file 标签默认样式的简单方法

    <html><head><title></title></head><body><form id="upload ...

随机推荐

  1. wepy - Cannot read property 'Promise' of undefined

    正当我们准备试探示例时,突然.... 造成这个错误有两个原因 (wepy以前的版本默认启动了Promise,自1.4.x以后需要手动开启) 1.未下载Promise 详情见启用文档:Promise   ...

  2. iOS Dev (50)用代码实现图片加圆角

    用代码实现图片加圆角:       iconView.layer.masksToBounds = YES;       iconView.;

  3. JSTL中c:forEach循环里的值的substr操作及对String操作的常用API

    <c:forEach items="${dataList}" var="item" varStatus="itemStatus"> ...

  4. 启动ip wizard时报the ip wizard does not support dhcp

    启动ip wizard时报the ip wizard does not support dhcp 阅读:5502012-05-11 11:15 标签:loadrunner 打开ip wizard:开始 ...

  5. Eclipse调试cas server 3.5.2.1

    由于在配置CAS+LDAP总是报错,决定Eclipse调试cas server,跟踪问题出在哪里? ================================================== ...

  6. spring cloud单点登录

    概述 基于springcloud的单点登录服务及基于zuul的网关服务(解决了通过zuul转发到认证服务之后session丢失问题) 详细 代码下载:http://www.demodashi.com/ ...

  7. Java菜鸟入坑学习要点

    一.掌握静态方法和属性 静态方法和属性用于描述某一类对象群体的特征,而不是单个对象的特征.Java中大量应用了静态方法和属性,这是一个通常的技巧.但是这种技巧在很多语言中不被频繁地使用.理解静态方法和 ...

  8. HDUOJ---1862EXCEL排序

    EXCEL排序 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  9. automake安装出错

    automake命令出错 configure.ac:64: error: possibly undefined macro: AM_ICONV    If this token and others ...

  10. Python练习笔记——对输入的数字进行加和

    请您输入数字,每个数字采用回车结束,当您输入型号*时,则结束数字输入,输出所有数字的总和 def num_sum(): i = 0 while True: get_num = input(" ...