如果我沒有記錯,HTML5允許你在一個隱藏的輸入元素上調用click
方法來通過一個自定義按鈕顯示文件對話框(爲什麼不只是讓它在沒有元素的情況下工作,我不知道)。不幸的是,並不是所有目前正在使用的瀏覽器都支持這個功能,所以你不能將樣式文件輸入看作是一個按鈕。
這是一個歡快的醜陋,但巧妙的CSS破解我碰上了一個網站一次(大概imgur):
.fileupload {
width: 100px;
height: 50px;
position: relative;
overflow: hidden;
background: ...; /* and other things to make it pretty */
}
.fileupload input {
position: absolute;
top: 0;
right: 0; /* not left, because only the right part of the input seems to
be clickable in some browser I can't remember */
cursor: pointer;
opacity: 0.0;
filter: alpha(opacity=0); /* and all the other old opacity stuff you
want to support */
font-size: 300px; /* wtf, but apparently the most reliable way to make
a large part of the input clickable in most browsers */
height: 200px;
}
和HTML去用它:
<div class="fileupload">
<input type="file" />
Any content here, perhaps button text
</div>
它做什麼它是否使文件輸入本身變得巨大(通過更改字體大小(!?))以確保它覆蓋按鈕,然後用overflow: hidden;
切斷多餘的文件。這可能不適用於絕對巨大的按鈕。
這是無用的,因爲在一些瀏覽器的文件名而來的按鈕之前,而在其他國家談到了。看到這裏:http://www.quirksmode.org/dom/pix/filefields.gif –
@Dbugger:IIRC Safari允許你點擊文本部分,以及許多其他瀏覽器。至少IE不喜歡該領域的點擊,因此是正確的定位。我檢查了一下,這實際上是imgur.com所做的,我很確定他們是一個相當不錯的現場測試。 –
看來,這些例子中的大多數與舊瀏覽器的例外工作...意味着IE 8和以下 – Paul