【前端】【探究】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 ...
随机推荐
- USB TCPM
USB TCPM(Type-C Port Manager)的主要作用是管理 USB Type-C 端口的连接和电源传输协议(USB Power Delivery, PD),确保设备正确识别.协商和切换 ...
- 43.v-if和v-for的优先级
v-for 的优先级高 延申问题:v-for 和 v-if 为什么不能在一起使用 ? 会造成性能的浪费,因为v-for 的优先级高,所以每次渲染都会执行v-if 判断条件,浪费时间 :比如 渲染 10 ...
- 《使用Gin框架构建分布式应用》阅读笔记:p127-p142
<用Gin框架构建分布式应用>学习第9天,p127-p142总结,总计16页. 一.技术总结 1.Authentication方式汇总 (1)API keys API keys 认证方式示 ...
- nginx配置高可用的集群
上图: (1)需要两台 nginx 服务器 (2)需要 keepalived (3)需要虚拟 ip 准备工作 配置高可用的准备工作 (1)需要两台服务器,两个Linux虚拟机即可,这里是 192.16 ...
- Nuxt.js 应用中的 components:extend 事件钩子详解
title: Nuxt.js 应用中的 components:extend 事件钩子详解 date: 2024/11/1 updated: 2024/11/1 author: cmdragon exc ...
- Issac_GYM重要过程记录
1 下载相关文件 进入github中下载相关的文件 https://github.com/leggedrobotics/legged_gym 2 加载自己绘制的URTL文件 这个链接用来下载宇树的Go ...
- 利用 canvas 实现签名效果
利用 canvas 实现签名效果 使用插件 jSignature github:https://github.com/brinley/jSignature 如果再H5 中使用需要加载 flash ...
- Docker配置Trojan代理
1.遇到的问题 在做云计算作业,使用阿里云的ECS服务器尝试使用docker拉取镜像的时候,发现一直无法从仓库拉取,更换了多个镜像源也没有解决问题,于是决定学会去配置linux的代理,记录过程. 2. ...
- Hello Markdown(完结)
Hello Markdown Markdown是一种轻量级的「标记语言」. 专注于文字内容: 纯文本,易读易写,可以方便地纳入版本控制: 语法简单,没有什么学习成本,能轻松在码字的同时做出美观大方的排 ...
- 使用YARP来实现负载均衡
YARP ("Yet Another Reverse Proxy") 是一个库,可帮助创建高性能.生产就绪且高度可自定义的反向代理服务器. YARP 是使用 ASP.NET 和 . ...