美化 file input 框

本文介绍了一种使用HTML、CSS及JavaScript技术美化网页中文件输入框的方法。通过隐藏默认的文件选择按钮并用自定义样式替代,同时保持了文件路径的显示功能。文中提供了一个完整的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 浏览器端显示           

    


2. 示例下载(免积分)


          美化file input.zip


3. 代码


<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>4</title>
    <script type="text/javascript" src="http://www.927car.com/js/jquery-1.11.1.js"></script>
<style>
* { margin: 0; padding: 0; }
input { border: none 0; }

.fileInputBox { width: 188px; overflow: hidden; position: relative;  margin: 100px auto; }
.fileInputBox * { float: left; }

.fileBtn { width: 52px; height: 22px; background: url("./images/browse_btn.jpg") no-repeat; font-family: arial; font-size: 12px; color: white; margin-left: 4px; }    
.fileDir { width: 130px; height: 20px; border: solid 1px #dedede; }

.fileInput { height: 22px; filter:alpha(opacity=00); -moz-opacity:0.0; -khtml-opacity: 0.0; opacity: 0.0; position: absolute; right: 0px; }

/*********** css hack ***********/
@-moz-document url-prefix() {
    .fileInput {
        left: 136px; 
    }
}
@media screen and (-webkit-min-device-pixel-ratio:0)    {
    .fileInput { 
        left: 136px; 
    }
}
/*********** css hack ***********/

</style>
<script>
jQuery(document).ready(function($) {
    var $fileInput = $(".fileInput");
    var $fileDir = $(".fileDir");
    var $fileBtn = $(".fileBtn");

    $fileInput.change(function(event) {
        $fileDir.attr('value', this.value);        
    });
});
</script>
</head>
<body>

    <div class="fileInputBox">
        
        <input class="fileInput" type="file"><!-- 被隐藏 -->

        <input class="fileDir" type="text"><!-- 显示文件路径 -->
        <input class="fileBtn" type="button" value="浏览"><!-- 替换后的浏览按钮 -->

    </div>

</body>
</html>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值