input type=file 上传文件样式美化

来源:https://www.jianshu.com/p/6390595e5a36

在做input文本上传时,由于html原生的上传按钮比较丑,需要对其进行美化,radio和checkbox类似

主要想到以下几种方法,欢迎大家补充

1. 通过position和opacity实现

  • input设置:透明度为0,position为绝对定位,font-size足够大
  • input外面套一层a或div等标签(此处以a举例),a设置:position为相对定位,overflow: hidden(为了避免在非a区域点击打开选择文件)

代码如下:

<html>

<head>
<style>
.ui-upload {
font-size: 14px;
width: 80px;
height: 30px;
line-height: 30px;
text-align: center;
position: relative;
cursor: pointer;
color: #fff;
background: #00abff;
border-radius: 3px;
overflow: hidden;
display: inline-block;
text-decoration: none;
} .ui-upload input {
position: absolute;
font-size: 100px;
right: 0;
top: 0;
opacity: 0;
filter: alpha(opacity=0);
cursor: pointer
}
</style>
</head> <body>
<a href="javascript:;" class="ui-upload">
<input type="file" />upload
</a>
</body> </html>

2. 通过label标签的for属性实现

for 属性规定了label与哪个表单元素进行绑定,包含显式联系和隐式联系两种

  • 显式联系:

以显式形式和控件联系起来,for属性的值和控件的id要保持一致

<label for="upload">upload</label>
<input type="file" id="upload" />
  • 隐式联系:

在 <label> 标签中放入控件隐式地连接起来

<label>uplaod<input type="file" /></label>

代码如下:

<html>

<head>
<style>
.ui-upload {
height: 30px;
width: 80px;
background-color: #00abff;
font-size: 14px;
line-height: 30px;
cursor: pointer;
display: inline-block;
text-align: center;
color: #fff;
border-radius: 3px;
margin-left: 20px;
}
</style>
</head> <body>
<label for="upload" class="ui-upload">upload</label>
<input type="file" id="upload" style="display: none;" />
<label class="ui-upload">upload<input type="file" style="display: none;" /></label>
</body> </html>

3. 通过onclick事件获取input操作
代码如下:

<html>

<head>
<style>
.ui-upload {
height: 30px;
width: 80px;
background-color: #00abff;
font-size: 14px;
line-height: 30px;
cursor: pointer;
display: inline-block;
text-align: center;
color: #fff;
border-radius: 3px;
border: 0px;
margin-left: 20px;
}
</style>
</head> <body>
<button class="ui-upload" onclick="document.getElementById('upload').click()">upload</button>
<input type="file" id="upload" style="display:none;" />
</body> </html>

结论

本文推荐大家使用第二种label标签实现,因为它不需要繁琐css定位,也不需要通过事件绑定。

input type=file 上传文件样式美化(转载)的更多相关文章

  1. 自定义type='file'上传文件样式

    改变默认的上传文件样式: 用label作为替代 <input id="file_-1" type="file" name="file" ...

  2. HTML <input type="file">上传文件——结合asp.net的一个文件上传示例

    HTML的代码:(关键是要在form里设置enctype="multipart/form-data",这样才能在提交表单时,将文件以二进制流的形式传输到服务器) 一. <fo ...

  3. input type file上传文件之后清空内容。

    上次写过如何上传文件,上传成功之后,会出现一些问题. 当我打开上传的文件,但是没有点击上传,然后关闭弹窗,接着继续上传刚才的那个文件.为了满足产品组的要求,我们一般都会把样式进行一定的覆盖. 但这就会 ...

  4. jquery判断 input type="file"上传文件是否为空

    要想获取type="file"的input内容,用var file = $("id").val();肯定是不行的,下面是代码: html上传按钮为: <i ...

  5. <input type="file">上传文件并添加路径到数据库

    注:这里是用的mvc所以没法用控件 html代码 <form method="post" enctype="multipart/form-data"> ...

  6. input type='file' 上传文件 判断图片的大小是否合格与witdh 和 height 是否合格

    function CheckFiles(obj) { var array = new Array('gif', 'jpeg', 'png', 'jpg'); //可以上传的文件类型 if (obj.v ...

  7. onclick事件触发 input type=“file” 上传文件

    添加按钮: <input type="button" name="button" value="浏览" onclick="j ...

  8. input[type=file]上传文件(格式判断、文件大小、上传成功后操作)

    var isUploadImg = false; //在input file内容改变的时候触发事件******************上传图片 $('#filed').change(function( ...

  9. 使用 input[type=file]上传文件

    var $file = $('#file'); $('#btn').click(function() { var data = new FormData(); data.append('file', ...

随机推荐

  1. Django 信号、中间件、i18n 专题

    信号 Django中提供了“信号调度”,用于在框架执行操作时解耦.通俗来讲,就是一些动作发生的时候,信号允许特定的发送者去提醒一些接受者. 1. Django内置信号 Model signals pr ...

  2. 编码符_new88

    begin#239B38F58D59E401465E1FEE0AFA7AE2DD920EB6645F4A2075C7ABBBE2141B925668C9D635D90DE884907F4E52F921 ...

  3. Python psutil模块使用

    import psutil # 获取内存信息 mem = psutil.virtual_memory() total = mem.total / 1024 / 1024 / 1024 used = m ...

  4. 组合拳出击-Self型XSS变废为宝

    前言 作者:米斯特安全攻防实验室-Vulkey_Chen 博客:gh0st.cn 这是一个鸡肋性质的研究,也许有些标题党,请见谅- 本文启发于一些讨论,和自己脑子里冒出来的想法. 组合拳搭配 Self ...

  5. Spark机器学习(上)

    1.机器学习概念 1.1 机器学习的定义 在维基百科上对机器学习提出以下几种定义: l“机器学习是一门人工智能的科学,该领域的主要研究对象是人工智能,特别是如何在经验学习中改善具体算法的性能”. l“ ...

  6. Javascript高级编程学习笔记(77)—— 表单(5)过滤输入

    在日常实践中,我们常常会需要用户按照某种规则输入数据 但是文本框在默认情况下缺少验证数据的手段,因此需要使用JS来完成此类过滤输入的操作 通过事件和DOM的结合手段就能够将普通的文本框转换为功能型控件 ...

  7. Python Tkinter 简单使用

    简单的一些实例,能够实现一般的功能就够用了 Tkinter: 创建顶层窗口: # -*- coding: utf-8 -*- from Tkinter import *   root = Tk() r ...

  8. python之集合(set)学习

    集合(set) 集合是一个无序的不重复元素序列,使用大括号({}).set()函数创建集合, 注意:创建一个空集合必须用set()而不是{},因为{}是用来创建一个空字典. 集合是无序的.不重复的.没 ...

  9. Python编程Day5——可变与不可变类型、数据类型整合

    一.可变与不可变类型1.可变类原值型:只改变,但id不变,证明就是在改变原值,是可变类型2.不可变类型:值改变,但id也跟着改变,证明是产生了新的值,是不可变类型 x= print(id(x)) x= ...

  10. 使用一年ESB感受

    ESB(Enterprise service bus)-----企业服务总线的简写. 目前使用的是openESB,Sun公司的开源社区提供的,集成在netbean中,使用glassFish服务器. 先 ...