input[type=file]样式的修改

本文介绍了如何修改input[type=file]元素的默认样式,通过设置opacity为零并使用固定尺寸的覆盖元素来实现自定义样式。同时,还提到了在上传文件后更新显示文本和图片的方法。

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

默认样式:

<input type="file" name="files" id="uploaddocumentfile" multiple="multiple"/>

 

修改后样式:

将input[type=file]的opacity设置为零,点击需要执行的部分用长宽设定死覆盖,自己写样式。

​
<style>
    .file-btn{
        width: 72px;
        height: 32px;
        background: rgba(247,247,247,1);
        border: 1px solid rgba(230,230,230,1);
        border-radius :4px;
        color: #7A79FF;
        font-size: 12px;
        text-align: center;
        display: inline-block;
    }
    .btn-behind{
        font-size: 12px;
        font-family: Source Han Sans CN;
        font-weight: 400;
        color: #333;
        padding-left: 8px;
    }
    #uploaddocumentfile{
        opacity: 0;
        margin-top: -30px;
        height: 120px;
        width: 70px;
    }
</style>

<div class="file-btn">选择文件
    <input type="file" name="files" id="uploaddocumentfile" multiple="multiple"/>
</div>
<div style="width: 170px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; margin: -32px 0 0 78px;">
    <span class="file-placeholder btn-behind">暂未选择</span>
</div>
<div style="margin-top: 12px;">
    <img class="file-pic" src="../../../../assets/img/upload.png"/>
</div>
<div style="width: 78px; text-align: center; overflow: hidden; white-space: nowrap; text-overflow: ellipsis;">
    <span class="file-placeholder btn-down">暂未选择</span>
</div>

上传文件之后更改显示的文字和图片。

// 上传文件,更改显示文字
$("#uploaddocumentfile").on("change", function(){
    let file = $("#uploaddocumentfile").val();
    let pos = file.lastIndexOf("\\");
    let _name  = file.substring(pos + 1);
    if(_name != ""){
        $('.file-placeholder').html(_name);
    }
    $(".file-pic").css("content", 'url("../../../../assets/img/file.png")');
    $(".file-pic").css("padding", '8px');
});

 

参考来源:https://www.jianshu.com/p/d09ca57efc78

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值