样式输入类型文件?[复制]

Style input type file? [duplicate]

提问人:Banshee 提问时间:2/6/2011 最后编辑:RivieraKidBanshee 更新时间:2/4/2016 访问量:187035

问:

是否可以在不担心浏览器兼容性的情况下设置文件类型的输入元素的样式?就我而言,我需要实现背景图像和圆形边框(1px),如果可能的话,按钮也应该是自定义的。

HTML CSS 文件 跨浏览器 输入

评论


答:

1赞 Paul Sweatte 11/24/2011 #1

使用 clip 属性以及不透明度、z 索引、绝对定位和一些浏览器过滤器,将文件输入放在所需的按钮上:

http://en.wikibooks.org/wiki/Cascading_Style_Sheets/Clipping

30赞 teshguru 3/15/2012 #2

请按照以下步骤操作,然后您可以为文件上传表单创建自定义样式:

1.) 这是简单的 HTML 表单(请阅读我在这里写的 HTML 注释)

    <form action="#type your action here" method="POST" enctype="multipart/form-data">
    <div id="yourBtn" style="height: 50px; width: 100px;border: 1px dashed #BBB; cursor:pointer;" onclick="getFile()">Click to upload!</div>
    <!-- this is your file input tag, so i hide it!-->
    <div style='height: 0px;width: 0px; overflow:hidden;'><input id="upfile" type="file" value="upload"/></div>
    <!-- here you can have file submit button or you can write a simple script to upload the file automatically-->
    <input type="submit" value='submit' >
    </form>

2.) 然后使用这个简单的脚本将 click 事件传递给文件输入标签。

    function getFile(){
        document.getElementById("upfile").click();
    }

现在,您可以使用任何类型的样式,而无需担心如何更改默认样式。 我非常清楚这一点,因为我一个半月来一直在尝试更改默认样式。相信我,这很难,因为不同的浏览器有不同的上传输入标签。因此,请使用此来构建您的自定义文件上传表单。这是完整的自动上传代码。

<html>
<style>
#yourBtn{
   position: relative;
   top: 150px;
   font-family: calibri;
   width: 150px;
   padding: 10px;
   -webkit-border-radius: 5px;
   -moz-border-radius: 5px;
   border: 1px dashed #BBB; 
   text-align: center;
   background-color: #DDD;
   cursor:pointer;
  }
</style>
<script type="text/javascript">
 function getFile(){
   document.getElementById("upfile").click();
 }
 function sub(obj){
    var file = obj.value;
    var fileName = file.split("\\");
    document.getElementById("yourBtn").innerHTML = fileName[fileName.length-1];
    document.myForm.submit();
    event.preventDefault();
  }
</script>
<body>
<center>
<form action="#type your action here" method="POST" enctype="multipart/form-data" name="myForm">
<div id="yourBtn" onclick="getFile()">click to upload a file</div>
<!-- this is your file input tag, so i hide it!-->
<!-- i used the onchange event to fire the form submission-->
<div style='height: 0px; width: 0px;overflow:hidden;'><input id="upfile" type="file" value="upload" onchange="sub(this)"/></div>
<!-- here you can have file submit button or you can write a simple script to upload the file automatically-->
<!-- <input type="submit" value='submit' > -->
</form>
</center>
</body>
</html>

评论

0赞 Slopeside Creative 4/28/2016
不错的解决方案。在输入上使用“display:none”而不是“height: 0px;宽度:0px;overflow:hidden;' 将其从 DOM/您的布局中删除。
8赞 Iris 3/29/2012 #3

通过 Jquery 提供相同的解决方案。如果页面中有多个文件输入,则有效。

$j(".filebutton").click(function() {
    var input = $j(this).next().find('input');
    input.click();
});

$j(".fileinput").change(function(){

    var file = $j(this).val();
    var fileName = file.split("\\");
    var pai =$j(this).parent().parent().prev();
    pai.html(fileName[fileName.length-1]);
    event.preventDefault();
});

评论

2赞 johannesMatevosyan 3/15/2016
你能为此发布一把小提琴吗?
3赞 Jonas Mølgaard 6/1/2012 #4

在谷歌上看了很长时间,尝试了几种解决方案,包括 CSS、JavaScript 和 JQuery,我发现他们中的大多数都使用图像作为按钮。其中一些很难使用,但我确实设法拼凑出了一些最终对我有用的东西。

对我来说,重要的部分是:

  • “浏览”按钮必须是“按钮”(而不是图像)。
  • 按钮必须具有悬停效果(以使其看起来不错)。
  • 文本和按钮的宽度必须易于调整。
  • 该解决方案必须在 IE8、FF、Chrome 和 Safari 中工作。

这是我想出的解决方案。并希望它也能对其他人有用。

更改.file_input_textbox的宽度以更改文本框的宽度。

更改.file_input_div、.file_input_button和.file_input_button_hover的宽度以更改按钮的宽度。您可能还需要对位置进行一些调整。我一直想不通为什么......

若要测试此解决方案,请创建一个新的 html 文件并将内容粘贴到其中。

<html>
<head>

<style type="text/css">

.file_input_textbox {height:25px;width:200px;float:left; }
.file_input_div     {position: relative;width:80px;height:26px;overflow: hidden; }
.file_input_button  {width: 80px;position:absolute;top:0px;
                     border:1px solid #F0F0EE;padding:2px 8px 2px 8px; font-weight:bold; height:25px; margin:0px; margin-right:5px; }
.file_input_button_hover{width:80px;position:absolute;top:0px;
                     border:1px solid #0A246A; background-color:#B2BBD0;padding:2px 8px 2px 8px; height:25px; margin:0px; font-weight:bold; margin-right:5px; }
.file_input_hidden  {font-size:45px;position:absolute;right:0px;top:0px;cursor:pointer;
                     opacity:0;filter:alpha(opacity=0);-ms-filter:"alpha(opacity=0)";-khtml-opacity:0;-moz-opacity:0; }
</style>
</head>
<body>
    <input type="text" id="fileName" class="file_input_textbox" readonly="readonly">
 <div class="file_input_div">
  <input id="fileInputButton" type="button" value="Browse" class="file_input_button" />
  <input type="file" class="file_input_hidden" 
      onchange="javascript: document.getElementById('fileName').value = this.value" 
      onmouseover="document.getElementById('fileInputButton').className='file_input_button_hover';"
      onmouseout="document.getElementById('fileInputButton').className='file_input_button';" />
</div>
</body>
</html>

评论

0赞 Picard 12/21/2015
小提琴:jsfiddle.net/gd4wuvga
3赞 Kerry Liu 3/3/2013 #5

这是一个简单的纯 css 解决方案,它可以创建一个一致的目标区域,并允许您随心所欲地设置人造元素的样式。

基本思想是这样的:

  1. 有两个“假”元素(文本输入/链接)作为真实文件输入的兄弟姐妹。绝对定位它们,使它们正好位于目标区域的顶部。
  2. 用 div 包装文件输入。将溢出设置为隐藏(这样文件输入就不会溢出),并使其完全符合您希望的目标区域的大小。
  3. 将文件输入的不透明度设置为 0,使其隐藏但仍可单击。给它一个大的字体,这样你就可以点击目标区域的所有部分。

这是 jsfiddle: http://jsfiddle.net/gwwar/nFLKU/

<form>
    <input id="faux" type="text" placeholder="Upload a file from your computer" />
    <a href="#" id="browse">Browse </a>
    <div id="wrapper">
        <input id="input" size="100" type="file" />
    </div>
</form>
0赞 Hassaan 3/18/2013 #6

用于设置任何类型的输入样式,select、textArea。uniform js plugin

URL 已 http://uniformjs.com/