如何美化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. android sdk 中跨平台部分

    跨平台部分: platforms samples sources system-images 这几个可以直接拷贝即可

  2. 浏览器端js处理or直接冗余至服务器php处理?

    w交给客户端浏览器js处理,减少向服务器的提交字节.精简处理逻辑.

  3. Ansible安装过程中常遇到的错误(FAQ)

    1.安装完成后允许命令报错 Traceback (most recent call last): File , in <module> (runner, results) = cli.ru ...

  4. Storm简介及使用

    一.Storm概述 网址:http://storm.apache.org/ Apache Storm是一个免费的开源分布式实时计算系统.Storm可以轻松可靠地处理无限数据流,实现Hadoop对批处理 ...

  5. 12.Project Fields to Return from Query-官方文档摘录

    1 插入例句 db.inventory.insertMany( [ { item: "journal", status: "A", size: { h: 14, ...

  6. git提交到远程虚拟机

    git到自己的虚拟机中第一步:打通git(一)Linux中(ip为10.1.8.1)1.安装git如:Ubuntu中安装gitapt install git 2.Ubuntu中添加git用户sudo ...

  7. python服务器环境搭建——安装相关软件

    在上一篇我们在本地的虚拟服务器上安装好CentOS7后,我们的python web服务.自定义的python service或python脚本需要在服务器上运行,还需要在服务器安装各种相关的软件才行, ...

  8. mysql监控优化(二)主从复制

    复制解决的基本问题是让一台服务器的数据和其他服务器保持同步.一台主服务器的数据可以同步到多台从服务器上.并且从服务器也可以被配置为另外一台服务器的主库.主库和从库之间可以有多种不同的组合方式. MyS ...

  9. Java中树和树的几种常规遍历方法

    其中包含有先序遍历.中序遍历.后序遍历以及广度优先遍历四种遍历树的方法: package com.ietree.basic.datastructure.tree.binarytree; import ...

  10. A simple windows programm in c

    A simple windows programm in c        The following programm is a minimal windows program. It opens ...