想到英语四级考了两次都没过,我觉得要多使用英文,所以本文使用英文书写。

本文讲述了遇到的问题,解决的思路,并讲述了解决方案,也许对你会有帮助。

2022年9月25日凌晨3点20分@萌狼蓝天

Problem description

when we want to use input of file type, it has a very serious problem : the text content(文本内容) displayed (显示)cannot be set by us , And the default text display varies(不同) from browser to browser!

  • It displays on Edge as shown below

  • It displays on Webstorm default preview browser(Webstorm默认预览浏览器) as shown below

Solution

We can use a button to select file and use a text box to show selected file's name , instead of file type input.

This does not mean that we discard input of file type , just hide it and use other elements to control it , it's still the one that actually works.(这并不意味着我们抛弃了文件类型的 "input",只是隐藏了它并使用其他元素来控制它,它仍然是真正起作用的)

Example

1.Use Button to select file

Use display:none to hiden file type input

 <input id="selectFile" type="button" value="选择文件" onclick="document.getElementById('fileUp').click()">
<input type="file" style="display: none" name="fileUp" id="fileUp">

When we click on the button with the id selectFile, Javascript will get the element of file type input, and then execute its click event(点击事件).

2.Use a box to show selected file‘s name

You can use a text type inputp tags、 div tags and so on.

Now,I'll use a text type input as case to write a demo

Prefor a text type input and set it read only, the code is as follows:

<input type="text" readonly id="fileSelected" value="未选择任何文件" >

Now, we have to thinking about how to get the selected file'name.

Obviously, we need to read the name of the uploaded file from the input of the file type, the code is as follows:

<input type="text" readonly id="fileSelected" value="未选择任何文件" onclick="document.getElementById('fileUp').click()" style="border: none;">

tips : you can use outline:none to make the element outline hiden.

The complete code

<input id="selectFile" type="button" value="选择文件" onclick="document.getElementById('fileUp').click()">
<label for="selectFile">
<input type="text" readonly id="fileSelected" value="未选择任何文件" onclick="document.getElementById('fileUp').click()" style="border: none;">
</label>
<input type="file" style="display: none" name="fileUp" id="fileUp"
onchange="document.getElementById('fileSelected').value=this.value">

【前端】【探究】HTML - input类型为file时如何实现自定义文本以更好的美化的更多相关文章

  1. 怎么去掉select的下拉箭头和输入框input类型为number时的上下箭头

    一.去掉select的下拉箭头 方法一:在select外面加一个div,设置select宽度大于div的宽度,并加一个超出隐藏属性overflow:hidden,小三角会隐藏掉: 方法二:给selec ...

  2. input输入框type=file时accept中可以限制的文件类型(转载)

    转载自: input type=file accept中可以限制的文件类型 在上传文件的时候,需要限制指定的文件类型. <input type="file" accept=& ...

  3. input类型为file改变默认按钮样式

    改变 input file 样式(input  文件域)是很多前端朋友经常遇到的头疼问题,今天推荐两种改变 input file 样式的两种常用方法: 方法一: <input type=&quo ...

  4. 在Chrome与火狐中,输入框input类型为number时,如何去除掉的自带的上下默认箭头

    如何移除input='number'时浏览器自带的上下箭头: CSS样式: /* 去除input[type=number]浏览器默认的icon显示 */ input::-webkit-outer-sp ...

  5. Input类型是checkbox时checked属性获取

    记录一下checkbox 的 checked 属性的获取办法,以备忘记: 假如你的一个HTML页中有这么一段代码: <input name="chbRem" id=" ...

  6. [HTML] css3 输入框input类型为number时,去掉上下箭头方式

    <input type="number" ...> <style> input::-webkit-outer-spin-button, input::-we ...

  7. 使用 HTML5 input 类型提升移动端输入体验

    在过去的几年里,在移动设备上浏览网页已变得难以置信的受欢迎. 但是这些设备上的浏览体验,有时遗留很多的有待改进.当涉及到填写表单时,这一点尤为明显.幸运的是,HTML5规范引入了许多新input类型, ...

  8. 使用 HTML5 input 类型提升移动端输入体验(键盘)

    在最近的项目中,策划老是要求我们弹出各种类型的键盘,特别是在iOS下,例如输入帐号的时候,不应该支持输入中文,该输入纯数字的时候就应该谈数字键盘等.个人觉得这些都是我们平时开发很少意识到的,虽然有些刁 ...

  9. 使用 HTML5 input 类型提升移动端输入体验(转翻译)

    在过去的几年里,在移动设备上浏览网页已变得难以置信的受欢迎. 但是这些设备上的浏览体验,有时遗留很多的有待改进.当涉及到填写表单时,这一点尤为明显.幸运的是,HTML5规范引入了许多新input类型, ...

  10. input 类型为number型时,maxlength不生效?

    input 类型为number型时,maxlength不生效? 可以加oninput属性来控制最大长度:<input id="numInput" type="num ...

随机推荐

  1. Linux内核虚拟内存管理之匿名映射缺页异常分析

    今天我们就来讨论下这种缺页异常,让大家彻底理解它.注:本文使用linux-5.0内核源代码.文章分为以下几节内容: 匿名映射缺页异常的触发情况 0页是什么?为什么使用0页? 源代码分析 3.1 触发条 ...

  2. 2022年10月中国数据库排行榜:达梦冲刺IPO热度不减,PolarDB立足创新夺锦才

    秋风萧瑟,洪波涌起. 2022年10月的 墨天轮中国数据库流行度排行榜 火热出炉,本月共有245个数据库参与排名,相比上月新增七个数据库,本月排行榜前十名变动较大:达梦反超openGauss重摘探花: ...

  3. 树形结构数据 数组对象 按照 sort 字段排序

    export function sortTreeData (data, flag) { let arr = [] if (flag === 1) { arr = data[0].children } ...

  4. 全局和局部混入 mixins

    使用 mixins 混入 的目的 :复用代码,维护代码 : 局部混入: 全局混入 + 按钮权限控制 : ps:定义一个方法 ,checkPermission (str) str 是按钮的权限标识 , ...

  5. 开发者故事:基于 KubeSphere LuBan 架构打造下一代云交付平台

    前言 在 KubeSphere Marketplace,个人开发者的创意和才能正在逐渐崭露头角.今日,我们荣幸地向大家介绍 Shipper 云交付平台的开发者--凌波,一位云原生领域的资深专家. 凌波 ...

  6. 云原生爱好者周刊:KubeSphere 3.3.0 Alpha 版发布

    开源项目推荐 Collection of Linux eBPF slides/documents 这个项目收集了各种与 eBPF 相关的资料,非常全面. magic-trace magic-trace ...

  7. 业务上线在即,ODBC应用程序性能频频掉线怎么搞?

  8. WEB 新手篇

    xctf在线场景以使用了,一直想写web题来着 001 view_scoure X老师让小宁同学查看一个网页的源代码,但小宁同学发现鼠标右键好像不管用了. 解: 查看网站源码,注释里有 flag 00 ...

  9. .NET周刊【10月第4期 2024-10-27】

    国内文章 C#实现信创国产Linux麦克风摄像头推流(源码,银河麒麟.统信UOS) https://www.cnblogs.com/shawshank/p/18494362 随着国际形势变化,软件信创 ...

  10. SpringBoot项目集成MinIO

    一.MinIO的下载安装以及基本使用 1.下载地址:https://dl.min.io/server/minio/release/windows-amd64/minio.exe 2.下载好后需要手动创 ...