input type="file" 样式的模仿

input type=file 附带按钮的样式几乎不被CSS所控制。在其它按钮应用了特殊格式之后,这种按钮在界面上显的极不协调。
就目前流行的几大浏览器来看,试图用样式表控制这种按钮的样式(或更改按钮上文字)都是不可能的。因而一些人采取隐藏input type=file而采用标准的按钮来代替它的方法。以下是一种比较简洁的实现,可以工作在IE中。其中的关键是,IE为Form控件提供了 click()方法,以便用程序指令替代人工点击。
<input type=file name=browse style="display: none;">
<input type=text name=file>
<input type=button style="color:white;background-color: #0066CC;height:20px; border: solid 1px #000; width: 80px;text-align:center;" onClick="browse.disabled=false;browse.click();file.value=browse.value;browse.disabled=true" value="Select a File">
但是这种方法不适合在firefox中使用
下面是另一种办法
  1. Take a normal <input type="file"> and put it in an element with position: relative.
  2. To this same parent element, add a normal <input> and an image, which have the correct styles. Position these elements absolutely, so that they occupy the same place as the <input type="file">.
  3. Set the z-index of the <input type="file"> to 2 so that it lies on top of the styled input/image.
  4. Finally, set the opacity of the <input type="file"> to 0. The <input type="file"> now becomes effectively invisible, and the styles input/image shines through, but you can still click on the "Browse" button. If the button is positioned on top of the image, the user appears to click on the image and gets the normal file selection window.
    (Note that you can't use visibility: hidden, because a truly invisible element is unclickable, too, and we need the <input type="file"> to remain clickable

Until here the effect can be achieved through pure CSS. However, one feature is still lacking.

  1. When the user has selected a file, the visible, fake input field should show the correct path to this file, as a normal <input type="file"> would. It's simply a matter of copying the new value of the <input type="file"> to the fake input field, but we need JavaScript to do this.

The HTML/CSS structure

I've decided on the following HTML/CSS approach:

div.fileinputs {
position: relative;
}

div.fakefile {
position: absolute;
top: 0px;
left: 0px;
z-index: 1;
}

input.file {
position: relative;
text-align: right;
-moz-opacity:0 ;
filter:alpha(opacity: 0);
opacity: 0;
z-index: 2;
}

<div class="fileinputs">
<input type="file" class="file" />
<div class="fakefile">
<input />
<img src="search.gif" />
</div>
</div>


原文:http://www.quirksmode.org/dom/inputfile.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: input type="file" 标签样式是无法改变的,因为它的样式是由操作系统的文件选择器控制的。但是你可以使用一些技巧来模拟一个自定义样式的文件选择器,比如使用 CSS 和 JavaScript 来隐藏原生的 input 元素,然后用一个自定义的按钮触发点击事件,再通过 JavaScript 来模拟文件选择器的操作。具体的实现方法可以参考一些开源的文件上传组件库。 ### 回答2: 要改变 input type=file样式,可以使用以下两种方法: 1. 使用样式丰富的外部库或框架:可以使用一些流行的CSS库或框架,如Bootstrap、Semantic UI等,它们提供了自定义文件输入按钮样式的选项。你可以使用它们提供的类或样式来覆盖默认的input type=file样式。具体的实现方法可以参考对应文档或示例。 2. 使用自定义样式和JavaScript:使用自定义样式和JavaScript可以更灵活地改变 input type=file样式,以下是一种常见的改变样式的方法: - 首先,隐藏默认的文件选择按钮:设置 input[type="file"] 的样式 display:none;,这样文件选择按钮将不会显示出来。 - 然后,在附近添加一个自定义的按钮或元素,例如一个按钮或一个带有图标的div。 - 当点击自定义按钮或元素时,通过 JavaScript 来触发 input type=file 的点击事件,使用户能够选择文件。可以使用下面的代码: (假设 input type=file 的 id 为 "fileInput") ```javascript document.getElementById("customButton").addEventListener("click", function() { document.getElementById("fileInput").click(); }); ``` - 最后,使用 CSS 来美化自定义按钮或元素的样式,例如改变颜色、添加边框等等。 需要注意的是,虽然可以改变 input type=file 的外观样式,但出于安全考虑,浏览器仍然会强制要求用户使用默认的文件选择对话框来选择文件,而不是通过自定义样式的按钮来选择文件。 ### 回答3: 要改变<input type=file>的样式,可以使用以下方法: 1. 使用CSS样式:可以通过对<input type=file>应用CSS样式来自定义其外观。通过设置元素的宽度、高度、边框、背景色等属性,可以改变文件输入框的外观。同时,还可以通过设置文字颜色、字体大小等属性来改变文件输入框中的提示文本。 2. 使用插件或框架:有一些第三方插件或框架提供了更丰富的文件输入框样式定制选项。例如,可以使用jQuery插件如Fileinput等来改变文件输入框的外观。这些插件通常提供了自定义按钮样式、预览功能等扩展功能。 3. 隐藏原始的文件输入框,自定义一个替代的元素:可以使用CSS和JavaScript来隐藏原始的<input type=file>元素,并创建一个自定义的替代元素。例如,可以创建一个按钮或图标,并通过JavaScript与文件输入框进行交互。当用户点击自定义元素时,可以模拟触发文件输入框的点击事件,并实现文件选择功能。 需要注意的是,由于安全限制,无法直接改变<input type=file>元素的样式。然而,通过上述方法可以实现对文件输入框的样式定制或自定义。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值