File input 的样式和文字的更改方法__适用于Firefox、IE等浏览器
编辑器加载中...每次写完的东西就忘了,下次用时还要重查资料重新写,这是今天写的一段测试代码,保留下来,记录给自已,同时分享给大家。 目标: 把下边的这个上传文件的input样式和文字改成一句话“上传图片”,同样实现上传图片的功能。 难点 1、不同浏览器的样式表现不同。 2、文字如果只用click这样的方式是不可以的。 3、input 上的文字是没有办法更改的。 解决的方法 用Javascript 尝试了很多种方法,都不行,很麻烦。最后在网上看到一个人写的一个办法,给了些启发,于是只用两行CSS来解决这个问题。 如果直接在文字上加onclick事件来实现input的click(),这时选中文件是可以的,但这样的方式在表单submit时就失效了。所以说,file input 必须是真正的点击才有效果。 于是,就想了个办法,把input 放在文字的上边,弄成透明的,这样在点文字时,实际是点击了input,这样就实现了文件的上传。 第一步 我把input 用CSS改成比较大的,放在文字的后边,这样点文字时,其实是点了Input。
<! doctype html public "-//w3c//dtd html 4.01//en" "http://www.w3.org/tr/html4/strict.dtd"> < html xmlns="http://www.w3.org/1999/xhtml"> < head > < meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> < title >input type file test - zishu.cn</ title > </ head > < style > #uploadImg{ font-size:12px; border:1px solid #CC0000; position:absolute} #file{ position:absolute; z-index:100; margin-left:-180px; font-size:60px;opacity:10;filter:alpha(opacity=10); margin-top:-5px;} </ style > < body > < span id="uploadImg"> < input type="file" id="file" size="1" > < a href="#">上传图片</ a > </ span > </ body > </ html > |
第二步
再更改一下CSS,把多出的Input部分隐藏就可以了。
<! doctype html public "-//w3c//dtd html 4.01//en" "http://www.w3.org/tr/html4/strict.dtd"> < html xmlns="http://www.w3.org/1999/xhtml"> < head > < meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> < title >input type file test - zishu.cn</ title > </ head > < style > #uploadImg{ font-size:12px; border:1px solid #CC0000; overflow:hidden; position:absolute} #file{ position:absolute; z-index:100; margin-left:-180px; font-size:60px;opacity:10;filter:alpha(opacity=10); margin-top:-5px;} </ style > < body > < span id="uploadImg"> < input type="file" id="file" size="1" > < a href="#">上传图片</ a > </ span > </ body > </ html > |
最终的效果
再把Input弄成百分百透明就可以了
<! doctype html public "-//w3c//dtd html 4.01//en" "http://www.w3.org/tr/html4/strict.dtd"> < html xmlns="http://www.w3.org/1999/xhtml"> < head > < meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> < title >input type file test - zishu.cn</ title > </ head > < style > #uploadImg{ font-size:12px; overflow:hidden; position:absolute} #file{ position:absolute; z-index:100; margin-left:-180px; font-size:60px;opacity:0;filter:alpha(opacity=0); margin-top:-5px;} </ style > < body > < span id="uploadImg"> < input type="file" id="file" size="1" > < a href="#">上传图片</ a > </ span > </ body > </ html > |
就是这样,知道了原理,你就可以随意的更改文字和样式了,改成图片,改成按钮都没有问题。 2、这种方法看上去有点土,但简单,实用,适用我电脑上的所有浏览器(ie6/ie7/firefox)。 File input 的样式和文字的更改方法__适用于Firefox、IE等浏览器 原文地址为:http://www.zishu.cn/09/859.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构