为input type="file"添加样式

3

我已经从多个地方了解到这个问题。

我尝试了在这里找到的方法:http://www.quirksmode.org/dom/inputfile.html

但我无法使其工作。因此,我尝试了一种CSS/HTML方法,在Firefox、Chrome、Safari和Opera(所有最新版本)中都能够完美运行,但在IE8中不能(你猜对了)。

它看起来很好,但单击时不起作用。如果双击它,它就会起作用。

有什么办法让它可以单击操作吗?

目前,我正在使用IE条件注释来告诉使用IE的用户双击选择按钮。该网站目前正在设计中,尚未向用户发布,希望在发布之前解决这个问题。


设置 input type="file" 的样式...FML :) - tybro0103
8个回答

6

一个很好的解决方案是使用label元素,以及它的"for"属性将其与输入框相连,从而实现单击操作。此解决方案的另一个重要元素是在输入框上应用filter:alpha(opacity=0)。

HTML:

 <label for="fileupload"> 
     <input id="fileupload" type="file" >   
     <p>click here</p>
 </label>

CSS:

label {
   background-color: #ECECEC;
   display: block;
   width: 600px;
   height: 600px;
   cursor: pointer;
}

input[type="file"] {
   width: 100%;
   height: 100%;
   z-index: 100;
   position: relative;
   opacity: 0;
   filter:alpha(opacity=0); 
   cursor: pointer;
 }

3
我有一个解决方案,可以帮助您按照所需的样式设置按钮,并在所有浏览器中只需单击一次即可。
基本上创建一个div并给它一个类或id,例如让我们称其为“outer_div”。
然后将文件输入放置在“outer_div”内,并为其指定一个id,例如此示例中称其为“file_input_name”。
接下来是CSS...
对于“outer_div”,请提供以下属性:
    #outer_div{
    cursor:pointer;
    overflow:hidden;
    display:block;
    float:left;
    position:relative;
    width:83px;
    height:25px;
    background:url(your button image goes here) no-repeat; 
    }

请确保您的按钮具有相同的宽度属性,以使此功能正常工作。

接下来是file_input_name的CSS:

  #file_input_name{
position:relative;
float:left;
cursor:pointer;
right:138px;
-moz-opacity:0 ;
filter:alpha(opacity: 0);
opacity: 0;
z-index: 2;
text-align: right;
        }

这应该能起作用。基本上的操作是将文件输入放在背景图像上方,而不是在文件URL处。
我自己已经这样做了,在IE和Firefox / Safari / Chrome中都可以正常工作。
如果需要更多帮助,请告诉我您的情况。

这并没有解决双击与单击的问题,仍然需要双击覆盖输入框的区域,而单击按钮的区域。 - Matilda

2
我认为我找到了两种解决方案来解决您的问题。您在.file类上使用:filter:alpha(opacity:0),因此隐藏了通常在上传控件中看到的实际“浏览...”按钮。
第一种解决方案是使用条件语句来设置CSS,当用户使用IE时,将替换您的精美“选择”图像为普通但已经过样式化的“浏览”按钮。
第二个解决方案是调整FileAttachment 输入的大小,使隐藏的“浏览...”按钮适合“选择”图像的位置。从那里开始,您只需要确保按钮的z-index属性高于图像的属性即可。
如果这些解决方案可以解决您的需求,请告诉我。
:D

你能在IE中为浏览按钮设置样式吗?怎么做?第二个方法没有起作用。如果将图像的z-index设置得比浏览按钮高,就无法单击浏览按钮。图像应该非常接近于浏览按钮的大小,如果不是完全相同的大小。 - L84
非常抱歉,我的意思是您应该将按钮的z-index设置得比图像高,而不是相反。我会立即编辑我的答案。顺便说一句,您的CSS已经对浏览按钮进行了样式设置,请删除.file类的filter属性并查看效果 :) - mario.tco
啊,我明白了,我的Z-Index比图片高。此外,我看到IE如何样式化文件输入。我可以接受这种方式,而不是使用我的“选择”图像。有没有办法更改文件输入,使其不显示黄色(在我的屏幕上)?谢谢! - L84
我没有看到你描述的黄色文件输入框 :/ 我在IE7、8和9中尝试了,但仍然看到它是灰色的。你能发给我一张截图吗? :) - mario.tco
如果您在屏幕上看到它是灰色的,那可能是我的电脑设置问题。这里有一张截图 - http://screencast.com/t/OH09OyYP 注意:在IE 7中,它是灰色的,在IE 8中,就像您在截图中看到的那样。 - L84
在实现第二个解决方案时,我也需要这个技巧,以使实际的文件上传“浏览”按钮占据父元素的全部空间 - https://dev59.com/1FjUa4cB1Zd3GeqPTaU6 - Spain Train

1
他们必须双击是因为那个是“文本区域”。
这里的第一张图片是IE8上传控件的图像。请注意,有文本框和按钮。用户单击按钮或双击文本框以调出按钮。
当你叠加它时,文本区域会重叠你想要单击的部分,导致需要双击。
下一个图像是相同的文件上传控件,没有不透明度,上面有一个按钮,并且具有来自IE8 DOM检查器的轮廓。如果我单击蓝色矩形的最右侧部分,单次单击有效,其他所有地方都需要双击。还请注意来自文件输入的文本编辑插入符号覆盖了按钮。
解决方法是调整/定位隐藏文件输入,将浏览按钮放置在所需位置。

1
使用一些 Bootstrap 魔法,我做到了,非常简单。
好的,在 head 标签中插入链接到 Bootstrap css 后,执行以下操作: <div class="fileUpload btn btn-primary"> <span>上传</span> <input type="file" class="upload" /> </div> 这将创建一个文件输入框,并使用 Bootstrap css 进行样式设计。
现在是棘手的部分:
  .fileUpload {
    position: relative;
    overflow: hidden;
    margin: 10px;
}
.fileUpload input.upload {
    position: absolute;
    top: 0;
    right: 0;
    margin: 0;
    padding: 0;
    font-size: 20px;
    cursor: pointer;
    opacity: 0;
    filter: alpha(opacity=0);
}

就这样,现在您拥有了一个带有样式的功能性按钮(文件输入类型)。

我发现这篇文章是因为我遇到了同样的问题,您可以查看更多信息。


0
如果您想隐藏默认的输入按钮,而又不失去IE8在输入文件上的默认行为以及跨浏览器支持,请尝试应用以下方法。
input.file{
   width:0 ; 
   height:0;
}  //supposing your file input has class of .file`   

在 CSS 中,你就可以开始了。


0

我已经有一段时间没有问这个问题了,下面是我现在用于样式文件输入的方法。这些信息来自Codrops。它提供了更详细的内容并提供了几个示例,但以下是基础知识:

HTML

 <input type="file" name="file" id="file" class="inputfile" />
 <label for="file">Choose a file</label> 

需要检查JS

<html class="no-js">
  <head>
      <!-- remove this if you use Modernizr -->
      <script>(function(e,t,n){var r=e.querySelectorAll("html")[0];r.className=r.className.replace(/(^|\s)no-js(\s|$)/,"$1js$2")})(document,window,0);</script>
  </head>
</html>

CSS

.js .inputfile {
    width: 0.1px;
    height: 0.1px;
    opacity: 0;
    overflow: hidden;
    position: absolute;
    z-index: -1;
}

.inputfile + label {
    max-width: 80%;
    font-size: 1.25rem;
    /* 20px */
    font-weight: 700;
    text-overflow: ellipsis;
    white-space: nowrap;
    cursor: pointer;
    display: inline-block;
    overflow: hidden;
    padding: 0.625rem 1.25rem;
    /* 10px 20px */
}

.no-js .inputfile + label {
    display: none;
}

.inputfile:focus + label,
.inputfile.has-focus + label {
    outline: 1px dotted #000;
    outline: -webkit-focus-ring-color auto 5px;
}

.inputfile + label * {
    /* pointer-events: none; */
    /* in case of FastClick lib use */
}

.inputfile + label svg {
    width: 1em;
    height: 1em;
    vertical-align: middle;
    fill: currentColor;
    margin-top: -0.25em;
    /* 4px */
    margin-right: 0.25em;
    /* 4px */
}

JS

;( function ( document, window, index )
{
    var inputs = document.querySelectorAll( '.inputfile' );
    Array.prototype.forEach.call( inputs, function( input )
    {
        var label    = input.nextElementSibling,
            labelVal = label.innerHTML;

        input.addEventListener( 'change', function( e )
        {
            var fileName = '';
            if( this.files && this.files.length > 1 )
                fileName = ( this.getAttribute( 'data-multiple-caption' ) || '' ).replace( '{count}', this.files.length );
            else
                fileName = e.target.value.split( '\\' ).pop();

            if( fileName )
                label.querySelector( 'span' ).innerHTML = fileName;
            else
                label.innerHTML = labelVal;
        });

        // Firefox bug fix
        input.addEventListener( 'focus', function(){ input.classList.add( 'has-focus' ); });
        input.addEventListener( 'blur', function(){ input.classList.remove( 'has-focus' ); });
    });
}( document, window, 0 ));

0
基本上这不是您的问题,实际上IE以不同的方式使用文件上传控件。 它将文件上传控件视为文本框和输入按钮,当您单击文件上传“浏览”或“选择”按钮时,它会打开打开对话框,但是当您单击文件上传文本时,它会将焦点放在文本框上,然后再次单击此文本框,它将打开打开对话框。 可以说这是IE中预定义的错误,因此我们无法解决此问题,大多数用户都知道这个问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接