修改 input file 样式

本文介绍了一种统一不同浏览器下Input File样式的方法,并提供两种实现方案。同时,文章还讨论了在进行异步文件上传时遇到的问题。

普通的 input file 样式,会根据浏览器的不同而显示不同的效果,IE显示的是,左边一个文本框,右边一个按钮的形式,谷歌显示的是,左边一个按钮,右边一个label,火狐与谷歌相同,但是按钮上的文字又不一样,网上的一些文章写修改file的样式,大多都是用一个文本框,一个按钮或是超链接等等其他的标签来替换,这种方法是可以达到input file一样的效果,但是,我在一个项目中,涉及到异步上传文件,在这中,我发现了一个问题,就是用上面的那种方法,上传时,无法将文件传输到服务器中,至于是什么原因,暂时还未不清楚。废话不多说,下面就是修改样式的方法:

1:

<style type="text/css">
.input_text{// 文本框样式

}
.input_button{// 按钮样式

}
.input_file{// input file 样式,宽高为0,或display:none
  width: 0px;
  height: 0px;
}
</style>
<script type="text/javascript">
function checkFile(){
  document.getElementById("file1").click();
}
</script>

<input type="text" class="input_text" id="txtFile" name="txtFile" value="" />
<input type="button" class="input_button" value="浏览..." onclick="checkFile()" />
<input type="file" class="input_file" id="file1" name="file1" onchange="document.getElementById('txtFile').value=this.value" />
2:(我用的方法。将file放在a中,并设置为透明,使用span文字)

a{

display:inline-block;

width:80px; height:32px;

background:red;

position:relative;

overflow:hidden;

}
a:hover{

background:green;

}
._label {
display:block;
position:relative;
}
._span {
position:absolute;
float:left;
line-height:30px;
left:10px;
cursor:text;
font-size: 16px;
color: #000000;
}
input{

position:absolute; 

right:0; 

top:0; 

font-size:100px;

opacity:0;

filter:alpha(opacity=0);

}
</style>
<a>
<lable class="_label"><span class="_span">浏览...</span><input type="file" name="file1"/></lable>
</a>


修改 inputfile 类型样式可采用以下几种方法: ### 使用 JavaScript 配合 HTML 处理 在实际开发中,由于 input 中的“浏览”按钮是由 HTML 根据 input 的 type 解析出来的,无法直接用 CSS 对其位置进行修改,可使用 JavaScript 配合 HTML 处理。主要思路是重写一个新样式,将默认样式设为不可见,在 JS 里调用,当点击新样式时,调用这个 input 的点击事件 [^1][^2]。 示例代码如下: ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>修改 input file 样式</title> <style> /* 自定义样式 */ .custom-button { background-color: #007BFF; color: white; padding: 10px 20px; border: none; cursor: pointer; } /* 隐藏默认的 input file */ #file { display: none; } </style> </head> <body> <input id="file" type="file" /> <button class="custom-button" onclick="clickFile()">选择文件</button> <script> function clickFile() { const input = document.querySelector('#file'); input.click(); } </script> </body> </html> ``` ### 添加按钮并调用点击函数 添加一个按钮,点击按钮时调用 input file 的点击函数。自定义 clickFile 函数,简单调用 file 的 click 函数即可 [^3]。 示例代码如下: ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>修改 input file 样式</title> </head> <body> <input id="file" type="file" /><br /> <input type="button" value="上传文件" class="btn" onclick="clickFile()" /> <script> function clickFile() { const input = document.querySelector('#file'); input.click(); } </script> </body> </html> ``` ### 使用 CSS 定义样式 可以使用 CSS 对 input file 进行样式定义,如设置位置、透明度、宽度等 [^4][^5]。 示例代码如下: ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>修改 input file 样式</title> <style> .file { position: absolute; top: 0; right: 80px; height: 24px; filter: alpha(opacity: 0); opacity: 0; width: 260px; } .file span { max-width: 6em; overflow: hidden; display: block; white-space: nowrap; text-overflow: ellipsis; } </style> </head> <body> <input type="file" class="file" /> </body> </html> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值