tang150110 2021-08-21 11:30 采纳率: 100%
浏览 92
已结题

关于css input[type=file] 样式美化,change事件怎么写?

最近网上学习只做了一个织梦表单。

按照教程修改了[type=file] 样式美化后,上传图片没有显示文件名。

于是按照教程修改了上传样式。

教程:
css input[type=file] 样式美化,input上传按钮美化_梦华空影的博客-CSDN博客_input type=file 美化 我们在做input文本上传的时候,html自带的上传按钮比较丑,如何对其进行美化呢? 思路: input file上传按钮的美化思路是,先把之前的按钮透明度opacity设置为0,然后,外层用div包裹,就实现了美化功能。 代码如下: DOM结构: https://blog.csdn.net/cuilei210/article/details/78842231

最后一步,change事件没有代码格式,这一步怎么编写代码呢?请师傅指教:

美化后显示文件名
上面美化,把默认显示的文件名也给隐藏掉了,那么如何显示文件名称呢?没关系,我们可以用jquery来获取文件的文件名。
我们可以写个change事件?change事件怎么写?

$(".a-upload").on("change","input[type='file']",function(){
    var filePath=$(this).val();
    if(filePath.indexOf("jpg")!=-1 || filePath.indexOf("png")!=-1){
        $(".fileerrorTip").html("").hide();
        var arr=filePath.split('\\');
        var fileName=arr[arr.length-1];
        $(".showFileName").html(fileName);
    }else{
        $(".showFileName").html("");
        $(".fileerrorTip").html("您未上传文件,或者您上传文件类型有误!").show();
        return false 
    }
})

  • 写回答

3条回答 默认 最新

  • CSDN专家-showbo 2021-08-21 13:31
    关注

    参考文章的html结构少了 .fileerrorTip.showFileName 这2个样式的容器,所以没反应。使用下面的代码就可以了,同时修改了示例代码存在多个上传控件统一设置内容的bug,有帮助麻烦点个采纳【本回答右上角】,谢谢~~有其他问题可以继续交流~

    img

    <style>
        /*a  upload */
        .a-upload {
            padding: 4px 10px;
            height: 20px;
            line-height: 20px;
            position: relative;
            cursor: pointer;
            color: #888;
            background: #fafafa;
            border: 1px solid #ddd;
            border-radius: 4px;
            overflow: hidden;
            display: inline-block;
            *display: inline;
            *zoom: 1
        }
    
        .a-upload input {
            position: absolute;
            font-size: 100px;
            right: 0;
            top: 0;
            opacity: 0;
            filter: alpha(opacity=0);
            cursor: pointer
        }
    
        .a-upload:hover {
            color: #444;
            background: #eee;
            border-color: #ccc;
            text-decoration: none
        }
    
        .file {
            position: relative;
            display: inline-block;
            background: #D0EEFF;
            border: 1px solid #99D3F5;
            border-radius: 4px;
            padding: 4px 12px;
            overflow: hidden;
            color: #1E88C7;
            text-decoration: none;
            text-indent: 0;
            line-height: 20px;
        }
    
        .file input {
            position: absolute;
            font-size: 100px;
            right: 0;
            top: 0;
            opacity: 0;
        }
    
        .file:hover {
            background: #AADFFD;
            border-color: #78C3F3;
            color: #004974;
            text-decoration: none;
        }
    </style>
    <a href="javascript:;" class="a-upload">
        <input type="file" name="" id="">点击这里上传文件
        <span class="fileerrorTip"></span>
        <span class="showFileName"></span>
    </a>
    
    <a href="javascript:;" class="file">
        选择文件
        <input type="file" name="" id="">
        <span class="fileerrorTip"></span>
        <span class="showFileName"></span>
    </a>
    
    <script src="https://g.csdnimg.cn/??lib/jquery/1.12.4/jquery.min.js"></script>
    <script>
        $(".a-upload,.file").on("change", "input[type='file']", function () {
            var el = $(this), filePath = el.val(), p = el.parent();;
            if (filePath.indexOf("jpg") != -1 || filePath.indexOf("png") != -1) {
                p.find(".fileerrorTip").html("").hide();
                var arr = filePath.split('\\');
                var fileName = arr[arr.length - 1];
                p.find(".showFileName").html(fileName);
            } else {
                p.find(".showFileName").html("");
                p.find(".fileerrorTip").html("您未上传文件,或者您上传文件类型有误!").show();
                return false
            }
        })
    
    </script>
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 8月31日
  • 已采纳回答 8月23日
  • 创建了问题 8月21日

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)