【前端】【探究】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 ...
随机推荐
- 40. diff 的新旧节点数组如何比较
根据唯一标识符key值,把新旧的节点比较,不同就更新到新节点,相同就复用就节点,然后生成新的 Vnode :
- 2024csp初赛总结
浙江27日下午1:30出分了,j组97,s组61.5,和估分一模一样,还好没有挂分.然后3点的时候上洛谷看了一下,全国分数线出了,j组89分,s组56分.那应该都过了,随后同学的成绩也出来了,sjx, ...
- C# 并发控制框架:单线程环境下实现每秒百万级调度
前言 在工业自动化和机器视觉领域,对实时性.可靠性和效率的要求越来越高.为了满足这些需求,我们开发了一款专为工业自动化运动控制和机器视觉流程开发设计的 C# 并发流程控制框架. 该框架不仅适用于各种工 ...
- ArgoWorkflow教程(七)---高效的步骤间文件共享策略
之前我们分析了使用 artifact 实现步骤间文件共享,今天分享一下如何使用 PVC 实现高效的步骤间文件共享. 1. 概述 之前在 artifact 篇我们演示了如何使用 artifact 实现步 ...
- MIT 6.002 Circuits and Electronics by Prof. Anant Agarwal
官网:MIT 2007 Circuits and Electronics 参考资料,课程讲义,课程PPT. 国内的电路像石群老师,罗先觉老师,讲的很细致,也许跟教材有关系,像电阻串并联的等效电阻就用了 ...
- C语言之常量
常量 常量,常量表达式和const关键字修饰的只读变量都存储在.rodata只读数据段中 1.字面值常量 literal 整形字面值 123, 0xff00ff 字符常量,类型总是 int, 'a', ...
- Vue绘制图片轮播组件【转载】
基本要求:页面加载,自动播放.鼠标悬停,停止播放.鼠标离开,继续播放.点击左右箭头切换上一张,下一张图片.下方小圆点显示当前位第几张图片. 示例代码: 结构html: <template> ...
- 谈一谈 Netty 的内存管理 —— 且看 Netty 如何实现 Java 版的 Jemalloc
本文基于 Netty 4.1.112.Final 版本进行讨论 在之前的Netty 系列中,笔者是以 4.1.56.Final 版本为基础和大家讨论的,那么从本文开始,笔者将用最新版本 4.1.112 ...
- Vmware Workstation的虚拟机如何通过宿主机的无线网卡和外部通信
今天需要在我的笔记本w10电脑上安装一个linux虚拟机,苦于我的w10是家庭版,没有hyper-v功能,所以安装了 vmware的workstation的软件,然后创建了虚拟机,但是总是搞不定如何让 ...
- 安装了多个python版本指定pip安装目录
#检查库安装的目录import os import numpy as np import pandas as pd print(os.path.dirname(np.__file__)) print( ...