如何美化input[type="file"]

基本思路是:

(1)首先在 input 外层套一个 div ;

(2)将 div 和 input 设置为一样大小(width和height);

(3)设置 div 为相对位置, input 为绝对位置,并将 input 的 top 和 right 设为 0 ;

这样, div 和 input 就重叠了,点击 div 就相当于点击 input ;

(4)设置 input 的 opacity 为 0 ,全透明,这样就只能看见 div 了;

至于 div 的样式就随便设置了。

html:

<form action="#" enctype="multipart/form-data" method="post">
<div class="fileupload">
<script>
function getFilename(){
var filename=document.getElementById("file").value;
if(filename==undefined||filename==""){
document.getElementById("filename").innerHTML="点击此处上传文件";
} else{
var fn=filename.substring(filename.lastIndexOf("\\")+1);
document.getElementById("filename").innerHTML=fn; //将截取后的文件名填充到span中
}
}
</script>
<span id="filename">点击此处上传文件</span>
<input type="file" name="file" id="file" onchange="getFilename()"/>
</div>
</form>

css:

 /*文件上传*/
.fileupload{
position: relative;
width:200px;
height:35px;
border:1px solid #66B3FF;
border-radius: 4px;
box-shadow: 1px 1px 5px #66B3FF;
line-height: 35px;
padding-left: 8px;
overflow: hidden;
}
.fileupload input{
position: absolute;
width:200px;
height:35px;
right:;
top:;
opacity:;
filter: alpha(opacity=0);
-ms-filter: 'alpha(opacity=0)';
}

注意:input[type="file"] 的文件路径是受保护的,用 js 获取不到,使用上传的插件可以获取到。(只是从前端的角度讲)

input[type="file"]的样式以及文件名的显示的更多相关文章

  1. 自定义input[type="file"]的样式

    input[type="file"]的样式在各个浏览器中的表现不尽相同: 1. chrome: 2. firefox: 3. opera: 4. ie: 5. edge: 另外,当 ...

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

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

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

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

  4. html中input type=file 改变样式

    <style> #uploadImg{ font-size:12px; overflow:hidden; position:absolute} #file{ position:absolu ...

  5. html input[type=file] css样式美化【转藏】

    相信做前端的都知道了,input[type=file]和其他输入标签不一样,它的事件必须是在触发自身元素上,而不能是其他元素.所以不能简单的把input隐藏,放个button上去. <a hre ...

  6. input(type='file')上传多张照片并显示,传到后台

    以下内容为网络摘抄和实践修改所得,如有雷同,请谅解!!!! 1.首先是前端页面代码: 其中,<input type="file" id="file_input&qu ...

  7. 对input type=file 修改样式

    效果图先给: 在html中涉及到文件选择的问题,文件选择使用 input(class="filter_input form-control" type="file) 但是 ...

  8. input[type='file']样式美化及实现图片预览

    前言 上传图片是常见的需求,多使用input标签.本文主要介绍 input标签的样式美化 和 实现图片预览. 用到的知识点有: 1.input标签的使用 2.filelist对象 和 file对象 3 ...

  9. 更改input【type=file】样式

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

随机推荐

  1. Using the FutureRequestExecutionService Based on classic (blocking) I/O handle a great number of concurrent connections is more important than performance in terms of a raw data throughput

    Chapter 7. Advanced topics http://hc.apache.org/httpcomponents-client-ga/tutorial/html/advanced.html ...

  2. Yii框架2.0的 验证码

    最近看了一个Yii的教程视频,是按1.1的版本讲的,我想用Yii2.0的框架也参考他的学习开发下,结果发现好多不一样的,现在就说说验证码的事 首先加入一个actions 做验证码图片的显示,实际事调用 ...

  3. nginx.conf常用配置解析

    一.全局配置 user username groupname 运行用户及用户组 worker_processes auto 启动进程,通常设置成和cpu数量相等的一个数值,默认为1.可以设置为auto ...

  4. 小技巧-如何加快github下载代码的速度(转)

    作为开发人员,github是大家的标配了,常常会苦恼于gitclone某个项目的时候速度太慢,看着控制台那几K十几K的龟速,吐血!! 原因很简单:github的CDN被伟大的墙屏蔽所致. 所以解决方案 ...

  5. 线程管理coroutine

    非常好用的协程库,也可以当作线程管理来用 #include "coroutine.h" #include <stdio.h> struct args { int n; ...

  6. JDB调试代码 20165324 何春江

    Java书本程序调试: 课上程序调试

  7. Bootstrap3的输入框数字点击修改效果

    <div class="container"><div class="page-header"><h3>Bootstrap ...

  8. Eclipase .自动提示

    打开eclipse→Windows→Preferences→Java→Editor→Content Assist 修改Auto Activation triggers for java的值为 .abc ...

  9. TortoiseSVN_1.9.1.267_x64版本控制系统(针对Visual SVN Server)使用简单介绍

    软件下载地址:TortoiseSVN(SVN客户端)64位 V1.9.1.267简体中文免费版 软件详细操作说明:TortoiseSVN使用说明书(超详细) 文章内容:此篇是简单记录如何从Visual ...

  10. [caffe]caffe资料收集

    1.caffe主页,有各种tutorial. 2.Evan Shelhamer的tutorial,包括视频.