【前端】【探究】HTML - input类型为file时如何实现自定义文本以更好的美化
想到英语四级考了两次都没过,我觉得要多使用英文,所以本文使用英文书写。
本文讲述了遇到的问题,解决的思路,并讲述了解决方案,也许对你会有帮助。
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:noneto hiden file typeinput
<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 input、p 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:noneto 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时如何实现自定义文本以更好的美化的更多相关文章
- 怎么去掉select的下拉箭头和输入框input类型为number时的上下箭头
一.去掉select的下拉箭头 方法一:在select外面加一个div,设置select宽度大于div的宽度,并加一个超出隐藏属性overflow:hidden,小三角会隐藏掉: 方法二:给selec ...
- input输入框type=file时accept中可以限制的文件类型(转载)
转载自: input type=file accept中可以限制的文件类型 在上传文件的时候,需要限制指定的文件类型. <input type="file" accept=& ...
- input类型为file改变默认按钮样式
改变 input file 样式(input 文件域)是很多前端朋友经常遇到的头疼问题,今天推荐两种改变 input file 样式的两种常用方法: 方法一: <input type=&quo ...
- 在Chrome与火狐中,输入框input类型为number时,如何去除掉的自带的上下默认箭头
如何移除input='number'时浏览器自带的上下箭头: CSS样式: /* 去除input[type=number]浏览器默认的icon显示 */ input::-webkit-outer-sp ...
- Input类型是checkbox时checked属性获取
记录一下checkbox 的 checked 属性的获取办法,以备忘记: 假如你的一个HTML页中有这么一段代码: <input name="chbRem" id=" ...
- [HTML] css3 输入框input类型为number时,去掉上下箭头方式
<input type="number" ...> <style> input::-webkit-outer-spin-button, input::-we ...
- 使用 HTML5 input 类型提升移动端输入体验
在过去的几年里,在移动设备上浏览网页已变得难以置信的受欢迎. 但是这些设备上的浏览体验,有时遗留很多的有待改进.当涉及到填写表单时,这一点尤为明显.幸运的是,HTML5规范引入了许多新input类型, ...
- 使用 HTML5 input 类型提升移动端输入体验(键盘)
在最近的项目中,策划老是要求我们弹出各种类型的键盘,特别是在iOS下,例如输入帐号的时候,不应该支持输入中文,该输入纯数字的时候就应该谈数字键盘等.个人觉得这些都是我们平时开发很少意识到的,虽然有些刁 ...
- 使用 HTML5 input 类型提升移动端输入体验(转翻译)
在过去的几年里,在移动设备上浏览网页已变得难以置信的受欢迎. 但是这些设备上的浏览体验,有时遗留很多的有待改进.当涉及到填写表单时,这一点尤为明显.幸运的是,HTML5规范引入了许多新input类型, ...
- input 类型为number型时,maxlength不生效?
input 类型为number型时,maxlength不生效? 可以加oninput属性来控制最大长度:<input id="numInput" type="num ...
随机推荐
- .net6 中 Blazor PageTitle 设置无效的解决方法
直接在 razor 页面里添加 <PageTitle>xxx</PageTitle> 标签无效时的解决方法 For using the <PageTitle> ta ...
- USB PD和USB TYPE-C 的区别
USB Power Delivery (USB PD) 和 USB Type-C 是两个不同但相关的技术标准,它们在功能和应用上有所区别. 1. USB Type-C 连接器标准: USB Type- ...
- linux 内核中READ_ONCE宏定义
在Linux内核编程中,READ_ONCE 宏用于确保从内存中读取一个变量的值时,编译器不会对这个读取操作进行优化,从而保证了读取操作的原子性.这个宏通常在需要防止编译器优化.多线程或中断上下文中使用 ...
- 001 C#配置多个版本Swagger说明
1. AddSwaggerGen AddSwaggerGen 是配置多个版本的swagger的关键 Path.Combine 当前项目运行的路径 UseSwaggerUI 主要分为 2 步骤 : 1 ...
- go: 在proto中使用oneof类型
在proto中,可以使用OneOf类型,使用一个字段存储不同类型的数据.类似go中的interface. 假设有proto如下,Val是一个OneOf数据类型,它可以为double/int/str.. ...
- 高效求解 n 个点之间的最大曼哈顿距离
平面上有 n 个点,如何求出任意两点的曼哈顿距离的最大值? 曼哈顿距离的公式为: \[d((x_1, y_1), (x_2, y_2)) = |x_1 - x_2| + |y_1 - y_2| \] ...
- Java编程案例(专题)
文章目录 案例一:买飞机票 案例二:开发验证码 案例三:评委打分 案例四:数字加密 案例五:数组拷贝 案例六:抢红包 案例七:找素数 案例八:模拟双色球 8.1 手动投注 8.2 随机开奖号码 8.3 ...
- 使用FastAPI整合Gradio和Django
大家好,我是每天分享AI应用的萤火君! 经常接触机器学习的同学可能都接触过Gradio这个框架,Gradio是一个基于Python的专门为机器学习项目创建的快速开发框架,可以让开发者快速发布自己的模型 ...
- 采用线性回归实现训练和预测(Python)
已知测得某块地,当温度处于15至40度之间时,数得某块草地上小花朵的数量和温度值的数据如下表所示.现在要来找出这些数据中蕴含的规律,用来预测其它未测温度时的小花朵的数量. 测得数据如下图所示: imp ...
- FPGA时序约束基础
一.时序约束的目的 由于实际信号在FPGA内部期间传输时,由于触发器等逻辑期间并非理想期间,因此不可避免地存在传输延时,这种延迟在高速工作频率.高逻辑级数时会造成后级触发器地建立时间和保持时间不满足, ...