样式输入类型文件?
是否可以设置文件类型的输入元素的样式而不用担心浏览器兼容性?就我而言,我需要实现背景图像和圆形边框(1px),如果可能的话,还应该自定义按钮。
Is it possible to style a input element of type file without worrying about browser compatibility? In my case I need to implement a background image and round border(1px), the button should also be customised if possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
按照以下步骤,您可以为文件上传表单创建自定义样式:
1.) 这是简单的 HTML 表单(请阅读下面我在此处编写的 HTML 注释)
2.) 然后使用这个简单的脚本将点击事件传递给文件输入标签。
现在您可以使用任何类型的样式,而不必担心如何更改默认样式。
我非常清楚这一点,因为一个半月以来我一直在尝试更改默认样式。相信我,这非常困难,因为不同的浏览器有不同的上传输入标签。因此,请使用这个来构建您的自定义文件上传表单。这是完整的自动上传代码。
Follow these steps then you can create custom styles for your file upload form:
1.) This is the simple HTML form(please read the HTML comments I have written here bellow)
2.) Then use this simple script to pass the click event to file input tag.
Now you can use any type of a styling without worrying how to change default styles.
I know this very well, because I have been trying to change the default styles for month and a half. believe me it's very hard because different browsers have different upload input tag. So use this one to build your custom file upload forms.Here is the full AUTOMATED UPLOAD code.
通过 Jquery 实现相同的解决方案。如果页面中有多个文件输入,则有效。
Same solution via Jquery. Works if you have more than one file input in the page.
在 Google 上查了很长时间,尝试了几种解决方案,包括 CSS、JavaScript 和 JQuery,我发现大多数都使用图像作为按钮。其中一些很难使用,但我确实设法拼凑出一些最终对我有用的东西。
对我来说重要的部分是:
这是我想出的解决方案。并希望它也对其他人有用。
更改 .file_input_textbox 的宽度以更改文本框的宽度。
更改 .file_input_div、.file_input_button 和 .file_input_button_hover 的宽度以更改按钮的宽度。您可能还需要对位置进行一些调整。我从来不明白为什么......
要测试这个解决方案,创建一个新的 html 文件并将内容粘贴到其中。
After looking around on Google for a long time, trying out several solutions, both CSS, JavaScript and JQuery, i found that most of them were using an Image as the button. Some of them were hard to use, but i did manage to piece together something that ended out working out for me.
The important parts for me was:
This is the solution i came up with. And hope it can be of use to others as well.
Change the width of .file_input_textbox to change the width of the textbox.
Change the width of both .file_input_div, .file_input_button and .file_input_button_hover to change the width of the button. You might need to tweak a bit on the positions also. I never figured out why...
To test this solution, make a new html file and paste the content into it.
这是一个简单的纯 CSS 解决方案,它创建一致的目标区域,并让您可以根据自己的喜好设置人造元素的样式。
基本思想是这样的:
这是jsfiddle: http://jsfiddle.net/gwwar/nFLKU/
Here's a simple css only solution, that creates a consistent target area, and lets you style your faux elements however you like.
The basic idea is this:
Here's the jsfiddle: http://jsfiddle.net/gwwar/nFLKU/
使用 Clip 属性以及不透明度、z-index、绝对定位和一些浏览器过滤器将文件输入放置在所需的按钮上:
http://en.wikibooks.org/wiki/Cascading_Style_Sheets/Clipping
Use the clip property along with opacity, z-index, absolute positioning, and some browser filters to place the file input over the desired button:
http://en.wikibooks.org/wiki/Cascading_Style_Sheets/Clipping
使用
uniform js插件
来设置任何类型的输入样式,选择,文本区域。网址是
http://uniformjs.com/
use
uniform js plugin
to style input of any type, select, textarea.The URL is
http://uniformjs.com/