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

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

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. axios 常见状态码

    '100': 'Continue', '101': 'SwitchingProtocols', '102': 'Processing', '103': 'EarlyHints', '200': 'Ok ...

  2. 4. 说一下ts

    TypeScript 是微软基于JavaScript开发的开源编程语言,是js的超集,扩展了js语法并添加了静态类型,可以兼容js所有的运行平台: js 是弱类型语言 , ts 是强类型语言 : js ...

  3. 【异常处理】Assistive Technology not found: com.sun.java.accessibility.AccessBridge

    十一回来之后,工作电脑上的抓包工具Charles突然启动不起来了,双击图标后,一闪而过,就没动静了. 不知道是不是因为之前安装了什么工具.软件引起的. 打开CMD命令行,跳转到目录下启动,提示:Ass ...

  4. mysql进阶-锁

    锁 概述 锁是计算机协调多个进程或线程并发访问某一资源的机制.在数据库中,除传统的计算资源(CPU.RAM.I/O)的争用以外,数据也是一种供许多用户共享的资源. 如何保证数据并发访问的一致性.有效性 ...

  5. 开启 Calico eBPF 数据平面实践

    简介 Calico 从 v3.13 开始,集成了 eBPF 数据平面. 关于什么是 eBPF, 以及 Calico 为什么引入了 eBPF , 并不是本篇文章的重点,感兴趣的朋友可以自行阅读相关文档. ...

  6. 云原生周刊:Istio 1.20.0 发布 | 2023.11.20

    开源项目推荐 DevPod DevPod 是一款纯客户端工具,可在任何后端基于 devcontainer.json 创建可重现的开发人员环境.每个开发者环境都在一个容器中运行,并通过 devconta ...

  7. count(*)、count(1)哪个更快?面试必问:通宵整理的十道经典MySQL必问面试题

    一.你是如何理解Count(*)和Count(1)的? 这两个并没有区别,不要觉得 count() 会查出全部字段,而 count(1) 不会.所以 count() 会更慢,你觉得 MySQL 作者会 ...

  8. Effective C++:把C++看作一个语言联邦

    C++曾经是从C with class发展而来的,但是现在随着异常.模板.STL等等的发展,C++已变得相当的多样与庞大,这一方面让C++功能强大无比的多重范式语言,上天入地无所不能:另一方面,也给学 ...

  9. Docker-compose 常用命令和模板

    docker-compose logs -f ##查看该容器的启动的日志打印(日志从头打印 docker logs -f container_id ##查看某一容器的启动的日志打印(日志从头打印) d ...

  10. MySQL,你只需要看这一篇文章就够了!PART04--完结篇

    MySQL--DAY04 索引 定义 索引是在数据库表的字段上添加的,是为了提高查询效率存在的一种机制. 一张表的一个字段可以添加一个索引,当然,多个字段联合起来也可以添加索引. 索引相当于一本书的目 ...