设置输入类型="文件"按钮的样式

Shi*_*and 674 html css file-io

如何设置输入type="file"按钮的样式.

Jos*_*ier 916

你不需要JavaScript!这是一个跨浏览器的解决方案:

看这个例子! - 适用于Chrome/FF/IE - (IE10/9/8/7)

最好的方法是使自定义标签元素的for属性附加到隐藏文件输入元素.(标签的for属性必须与文件元素相匹配id才能使其生效).

<label for="file-upload" class="custom-file-upload">
    Custom Upload
</label>
<input id="file-upload" type="file"/>
Run Code Online (Sandbox Code Playgroud)

作为替代方案,您也可以直接使用标签包装文件输入元素:( 示例)

<label class="custom-file-upload">
    <input type="file"/>
    Custom Upload
</label>
Run Code Online (Sandbox Code Playgroud)

在样式方面,只需使用属性选择器隐藏1输入元素.

input[type="file"] {
    display: none;
}
Run Code Online (Sandbox Code Playgroud)

然后,您需要做的就是设置自定义label元素的样式.(例子).

.custom-file-upload {
    border: 1px solid #ccc;
    display: inline-block;
    padding: 6px 12px;
    cursor: pointer;
}
Run Code Online (Sandbox Code Playgroud)

1 - 值得注意的是,如果使用隐藏元素display: none,它将无法在IE8及以下版本中使用.另请注意,jQuery validate 默认情况下不验证隐藏字段.如果任这些东西都是你的一个问题,这里有两个不同的方法来隐藏输入(1,2在这种情况下)的工作.

  • 我确实对这个问题有疑问,在选择文件时我们将如何进行显示文件名? (36认同)
  • 很棒的解决方案,但你确实需要javascript.如果要像其他人一样要显示文件名,则需要在标签和隐藏输入之间添加范围:<span id ="file-selected"> </ span>.然后更新change事件的span:$('#file-upload').bind('change',function(){var fileName =''; fileName = $(this).val(); $('# file-selected').html(fileName);}) (27认同)
  • 如果为`input [type = file]`设置`display:none`,如何显示所选文件的名称? (14认同)
  • 你应该使用`position:absolute; 由于可访问性原因,左:-99999rem`而不是`display:none`.大多数情况下,如果使用`display:none`方法隐藏元素,屏幕阅读器将不会读取元素. (8认同)
  • 我很惊讶地发现似乎没有人考虑过键盘的可访问性.`label`元素不是键盘可访问的,不像`button`s和`input`s.添加`tabindex`不是一个解决方案,因为`label`在有焦点并且用户按下回车时仍然不会被操作.我通过*在视觉上隐藏输入来解决这个问题,所以它仍然可以聚焦,然后在`label`父级上使用`:focus-within`:http://jsbin.com/fokexoc/2/edit?html, CSS,输出 (5认同)
  • 至少在 Chrome 和 FF 中,您可以将文件拖放到输入元素上。有了这个解决方案,你就失去了这种令人讨厌的能力。有没有人遇到过这个问题并找到解决方案? (2认同)
  • 要添加到@Sam的注释中,以避免出现“ fakepath”问题,请将最后一行更改为'$('#file-selected')。html(fileName.replace(/^.*\\/,“”))); ' (2认同)
  • 您不必使用 jQuery,只需像 @Sam 所说的那样添加跨度,然后将 `onchange="showname()"` 添加到您的 **input 类型文件**。确保添加了获取文件名的函数:`function showname() { var name = document.getElementById('datei'); document.getElementById("file-selected").innerHTML = name.files.item(0).name; };`。在这里阅读时我想到了这一点:/sf/ask/153273081/ (2认同)
  • @OliverJosephAsh:作为一个与很多 PM 就 a11y 的重要性争论不休的人,恐怕我一点也不感到惊讶。 (2认同)
  • @Capsule 隐藏事物同时仍可访问的另一种方法是“不透明度:0” (2认同)

Jon*_*att 207

造型文件输入是众所周知的困难,因为大多数浏览器不会改变css或javascript的外观.

即使输入的大小也不会响应:

<input type="file" style="width:200px">
Run Code Online (Sandbox Code Playgroud)

相反,您将需要使用size属性:

<input type="file" size="60" />
Run Code Online (Sandbox Code Playgroud)

对于任何比这更复杂的样式(例如,更改浏览按钮的外观),您将需要查看在原生文件输入顶部覆盖样式按钮和输入框的狡猾方法.rm在www.quirksmode.org/dom/inputfile.html上已经提到过的文章是我见过的最好的文章.

  • 对于任何对现代方法感兴趣的人,我建议看[**这个答案**](http://stackoverflow.com/questions/572768/styling-an-input-type-file-button/25825731#25825731 ).它也不像其他一些答案那样需要JavaScript. (9认同)
  • 刚刚在这里找到了一个基于这个脚本的jquery解决方案:http://blog.vworld.at/2008/08/21/styling-an-input-typefile-using-jquery/ (6认同)
  • 这里解释了一个比quirksmode更简单的解决方案[http://stackoverflow.com/a/21842275/1256925].只是把这个链接放在这里,因为这个答案基本上只是一个链接答案. (3认同)
  • @JoshCrozier发布了一个非常好的解决方案.甚至打败假鼠标解决方案:) (3认同)

tes*_*uru 192

按照这些步骤,您可以为文件上载表单创建自定义样式:

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>
Run Code Online (Sandbox Code Playgroud)

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

function getFile(){
     document.getElementById("upfile").click();
}
Run Code Online (Sandbox Code Playgroud)

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

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();
}
Run Code Online (Sandbox Code Playgroud)

请享用!

祝你今天愉快,

  • 在IE10(可能还有IE9等)中,如果您尝试通过javascript单击文件输入按钮后自动提交表单,您将获得"Access is Denied"(或jQuery没有响应).因此,只要用户仍然是提交表单的用户,此方法就可以用于样式化文件输入按钮.花了一些时间来找到这个,我看到其他人也有同样的问题.有关详细信息,请参阅此http://stackoverflow.com/a/4335390/21579. (10认同)
  • 我在IE9中提交表单时遇到了麻烦.我收到一个'Access is Denied'错误,尝试通过javascript提交表单.如果我通过UI单击提交按钮,它可以工作.有没有解决这个问题? (7认同)
  • @ user1053263 - >感谢您的推荐..这里我使用了一个非常简单的java脚本,不需要使用像Jquery或PrototypeJS这样的框架. (6认同)
  • 适用于我尝试过的每一款浏览器,重量轻,容易. (3认同)

ADA*_*MJR 158

::file-selector-button

https://developer.mozilla.org/en-US/docs/Web/CSS/::file-selector-button

这是一个新的选择器,可用于设置文件选择器按钮的样式。

它完全支持最新的浏览器版本。

input[type=file]::file-selector-button {
  border: 2px solid #6c5ce7;
  padding: .2em .4em;
  border-radius: .2em;
  background-color: #a29bfe;
  transition: 1s;
}

input[type=file]::file-selector-button:hover {
  background-color: #81ecec;
  border: 2px solid #00cec9;
}
Run Code Online (Sandbox Code Playgroud)
<form>
  <label for="fileUpload">Upload file</label>
  <input type="file" id="fileUpload">
</form>
Run Code Online (Sandbox Code Playgroud)

这是演示不同样式的另一个片段:

.input_container {
  border: 1px solid #e5e5e5;
}

input[type=file]::file-selector-button {
  background-color: #fff;
  color: #000;
  border: 0px;
  border-right: 1px solid #e5e5e5;
  padding: 10px 15px;
  margin-right: 20px;
  transition: .5s;
}

input[type=file]::file-selector-button:hover {
  background-color: #eee;
  border: 0px;
  border-right: 1px solid #e5e5e5;
}
Run Code Online (Sandbox Code Playgroud)
<form>
  <div class="input_container">
    <input type="file" id="fileUpload">
  </div>
</form>
Run Code Online (Sandbox Code Playgroud)

我觉得这个答案是必要的,因为这里的大多数答案都已经过时了。


自定义输入文本

如果您想自定义文件选择器按钮的文本,这里有一个片段:

document.querySelector("#files").onchange = function() {
  const fileName = this.files[0]?.name;
  const label = document.querySelector("label[for=files]");
  label.innerText = fileName ?? "Browse Files";
};
Run Code Online (Sandbox Code Playgroud)
label {
  border: 1px solid #e5e5e5;
  border-radius: 10px;
  padding: 5px 10px;
  font-family: 'Helvetica', sans-serif;
  transition: .5s;
}

label:hover {
  background-color: #eee;
}
Run Code Online (Sandbox Code Playgroud)
<div class="input_container">
  <label for="files" class="btn">Browse Files</label>
  <input id="files" style="display:none;" type="file">
</div>
Run Code Online (Sandbox Code Playgroud)

为了简单起见,我更喜欢使用普通解决方案。如果您想要上述代码片段的 jQuery 版本,请查看这篇文章。

  • 这确实是人们想要的答案。非常感谢 !点赞! (10认同)

Rya*_*yan 76

使用css隐藏它并使用带有$(selector).click()的自定义按钮来激活浏览按钮.然后设置一个间隔来检查文件输入类型的值.间隔可以显示用户的值,以便用户可以看到上传的内容.提交表单时间隔会清除[编辑]抱歉我一直很忙意思是更新这篇文章,这里有一个例子

<form action="uploadScript.php" method="post" enctype="multipart/form-data">
<div>
    <!-- filename to display to the user -->
    <p id="file-name" class="margin-10 bold-10"></p>

    <!-- Hide this from the users view with css display:none; -->
    <input class="display-none" id="file-type" type="file" size="4" name="file"/>

    <!-- Style this button with type image or css whatever you wish -->
    <input id="browse-click" type="button" class="button" value="Browse for files"/>

    <!-- submit button -->
    <input type="submit" class="button" value="Change"/>
</div>
Run Code Online (Sandbox Code Playgroud)

$(window).load(function () {
    var intervalFunc = function () {
        $('#file-name').html($('#file-type').val());
    };
    $('#browse-click').on('click', function () { // use .live() for older versions of jQuery
        $('#file-type').click();
        setInterval(intervalFunc, 1);
        return false;
    });
});
Run Code Online (Sandbox Code Playgroud)

  • 请注意,如果您使用这种方式,它将在Internet Explorer中中断,因为它是一个安全例外. (7认同)
  • 这要聪明得多.希望我先读这个.我愚弄了其他解决方案,直到我自己想出来......太晚了.:) (2认同)
  • 尝试在jquery中使用trigger方法.另外.live在新的jquery中更改为.on() (2认同)

小智 60

HTML

<div class="new_Btn">SelectPicture</div><br>
<input id="html_btn" type='file'" /><br>
Run Code Online (Sandbox Code Playgroud)

CSS

.new_Btn {
// your css propterties
}

#html_btn {
 display:none;
}
Run Code Online (Sandbox Code Playgroud)

jQuery的

$('.new_Btn').bind("click" , function () {
        $('#html_btn').click();
    });
//edit: 6/20/2014: Be sure to use ".on" not ".bind" for newer versions of jQuery
Run Code Online (Sandbox Code Playgroud)

小提琴:http://jsfiddle.net/M7BXC/

你可以在没有普通JavaScript的jQuery的情况下实现你的目标.

现在newBtn与html_btn链接,你可以像你想要的那样为你的新btn设置样式:D

  • @MehdiKaramosly甚至可以在IE 6上运行 - 但这是真的**只有jQuery 1.x**,jQuery 2.x +不支持旧的IE (2认同)

Ans*_*elm 54

<input type="file">创建时,所有渲染引擎都会自动生成一个按钮.从历史上看,该按钮完全不具有风格.但是,Trident和WebKit通过伪元素添加了钩子.

三叉戟

从IE10开始,可以使用::-ms-browse伪元素设置文件输入按钮的样式.基本上,您应用于常规按钮的任何CSS规则都可以应用于伪元素.例如:

::-ms-browse {
  background: black;
  color: red;
  padding: 1em;
}
Run Code Online (Sandbox Code Playgroud)
<input type="file">
Run Code Online (Sandbox Code Playgroud)

在Windows 8上的IE10中显示如下:

在Windows 8上的IE10中显示如下:

WebKit的

WebKit为其文件输入按钮提供了一个带有::-webkit-file-upload-button伪元素的钩子.同样,几乎可以应用任何CSS规则,因此Trident示例也适用于此:

::-webkit-file-upload-button {
  background: black;
  color: red;
  padding: 1em;
}
Run Code Online (Sandbox Code Playgroud)
<input type="file">
Run Code Online (Sandbox Code Playgroud)

这在OS X上的Chrome 26中显示如下:

这在OS X上的Chrome 26中显示如下:

  • 而对于Firefox? (3认同)
  • 在您的回答中,使用 css,如何隐藏 &lt;input type=file&gt; 标记中的“未选择文件”字样。请评论我。 (2认同)
  • 来源:http://tjvantoll.com/2013/04/15/list-of-pseudo-elements-to-style-form-controls/ (2认同)
  • 如果有人想玩风格,我在这里添加了一个 jsfidde:http://jsfiddle.net/peter_drinnan/r0gq6kw2/ (2认同)

JDa*_*awg 23

如果您使用的是Bootstrap 3,这对我有用:

http://www.abeautifulsite.net/whipping-file-inputs-into-shape-with-bootstrap-3/

.btn-file {
  position: relative;
  overflow: hidden;
}
.btn-file input[type=file] {
  position: absolute;
  top: 0;
  right: 0;
  min-width: 100%;
  min-height: 100%;
  font-size: 100px;
  text-align: right;
  filter: alpha(opacity=0);
  opacity: 0;
  outline: none;
  background: white;
  cursor: inherit;
  display: block;
}
Run Code Online (Sandbox Code Playgroud)
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />

<span class="btn btn-primary btn-file">
    Browse...<input type="file">
</span>
Run Code Online (Sandbox Code Playgroud)

其中产生以下文件输入按钮:

示例按钮

说真的,请查看http://www.abeautifulsite.net/whipping-file-inputs-into-shape-with-bootstrap-3/

  • 这保留了拖放功能,这很棒:),大多数答案不接受拖放的文件。 (2认同)

Bal*_*hir 22

只有 CSS

使用这个非常简单EASY

.choose::-webkit-file-upload-button {
  color: white;
  display: inline-block;
  background: #1CB6E0;
  border: none;
  padding: 7px 15px;
  font-weight: 700;
  border-radius: 3px;
  white-space: nowrap;
  cursor: pointer;
  font-size: 10pt;
}
Run Code Online (Sandbox Code Playgroud)
<label>Attach your screenshort</label>
<input type="file" multiple class="choose">
Run Code Online (Sandbox Code Playgroud)

  • 简单但并非所有地方都支持 (2认同)

Kar*_*hik 22

我可以使用下面的代码用纯CSS做到这一点.我使用过bootstrap和font-awesome.

<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css" rel="stylesheet" />

<label class="btn btn-default btn-sm center-block btn-file">
  <i class="fa fa-upload fa-2x" aria-hidden="true"></i>
  <input type="file" style="display: none;">
</label>
Run Code Online (Sandbox Code Playgroud)


kev*_*cha 21

这里的工作示例使用本机拖放支持:https://jsfiddle.net/ms3bbazv/4/

在设置文件输入样式时,不应该破坏输入提供的任何本机交互.

display: none方法打破了本机拖放支持.

要不破坏任何内容,您应该使用opacity: 0方法进行输入,并使用包装中的相对/绝对模式对其进行定位.

使用此技术,您可以轻松地为用户设置单击/拖放区域的样式,并在dragenter事件的javascript中添加自定义类以更新样式并向用户提供反馈以让他看到他可以删除文件.

HTML:

<label for="test">
  <div>Click or drop something here</div>
  <input type="file" id="test">
</label>
Run Code Online (Sandbox Code Playgroud)

CSS:

input[type="file"] {
  position: absolute;
  left: 0;
  opacity: 0;
  top: 0;
  bottom: 0;
  width: 100%;
}

div {
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #ccc;
  border: 3px dotted #bebebe;
  border-radius: 10px;
}

label {
  display: inline-block;
  position: relative;
  height: 100px;
  width: 400px;
}
Run Code Online (Sandbox Code Playgroud)

这是一个工作示例(使用额外的JS来处理dragover事件和删除文件).

https://jsfiddle.net/ms3bbazv/4/

希望这有帮助!


小智 20

 <label>
    <input type="file" />
 </label>
Run Code Online (Sandbox Code Playgroud)

您可以将输入类型="文件"包装在输入的标签内.根据您的喜好设置标签样式,并使用display:none隐藏输入;


cod*_*leb 15

这种方法为您提供了整体的灵活性!ES6 / VanillaJS!

html:

<input type="file" style="display:none;"></input>
<button>Upload file</button>
Run Code Online (Sandbox Code Playgroud)

javascript:

document.querySelector('button').addEventListener('click', () => {
  document.querySelector('input[type="file"]').click();
});
Run Code Online (Sandbox Code Playgroud)

这隐藏了输入文件按钮,但在引擎盖下从另一个普通按钮单击它,您显然可以像任何其他按钮一样设置样式。除了无用的 DOM 节点之外,这是唯一没有缺点的解决方案。由于display:none;,输入按钮不会在 DOM 中保留任何可见空间。

(我不知道该向谁提供道具了。但我从 Stackoverflow 上的某个地方得到了这个想法。)


TLK*_*TLK 11

使用jquery很简单.通过略微修改给出Ryan建议的代码示例.

基本的HTML:

<div id="image_icon"></div>
<div id="filename"></div>
<input id="the_real_file_input" name="foobar" type="file">
Run Code Online (Sandbox Code Playgroud)

准备就绪时,请务必在输入上设置样式:opacity: 0 您无法设置,display: none因为它需要可点击.但是如果你愿意的话,你可以将它放在"新"按钮下面,或者用z-index收集其他东西.

单击图像时,设置一些jquery以单击实际输入.

$('#image_icon').click(function() {
    $('#the_real_file_input').click();
});
Run Code Online (Sandbox Code Playgroud)

现在你的按钮正常工作.只需在更改时剪切并粘贴值即可.

$('input[type=file]').bind('change', function() {
    var str = "";
    str = $(this).val();
    $("#filename").text(str);
}).change();
Run Code Online (Sandbox Code Playgroud)

哇哇!您可能需要将val()解析为更有意义的内容,但您应该全部设置.

  • 好主意,但这不适用于IE浏览器.$('#the_real_file_input').click()将触发打开的对话框,但不会将文件选入表单,上传将失败. (2认同)

Sat*_*rny 9

这是一个解决方案,它没有真正设置<input type="file" />元素的样式,而是<input type="file" />在其他元素(可以设置样式)的顶部使用元素.该<input type="file" />元素是不是真的可见的,因此,总的错觉是一个很好的样式文件上传控制.

我最近遇到了这个问题,尽管Stack Overflow上有很多答案,但似乎没有一个真的符合这个要求.最后,我最终定制了这个,以便有一个简单而优雅的解决方案.

我也在Firefox,IE(11,10和9),Chrome和Opera,iPad以及一些Android设备上进行了测试.

这是JSFiddle链接 - > http://jsfiddle.net/umhva747/

$('input[type=file]').change(function(e) {
    $in = $(this);
    $in.next().html($in.val());
    
});

$('.uploadButton').click(function() {
    var fileName = $("#fileUpload").val();
    if (fileName) {
        alert(fileName + " can be uploaded.");
    }
    else {
        alert("Please select a file to upload");
    }
});
Run Code Online (Sandbox Code Playgroud)
body {
    background-color:Black;
}

div.upload {
    background-color:#fff;
    border: 1px solid #ddd;
    border-radius:5px;
    display:inline-block;
    height: 30px;
    padding:3px 40px 3px 3px;
    position:relative;
    width: auto;
}

div.upload:hover {
    opacity:0.95;
}

div.upload input[type="file"] {
    display: input-block;
    width: 100%;
    height: 30px;
    opacity: 0;
    cursor:pointer;
    position:absolute;
    left:0;
}
.uploadButton {
    background-color: #425F9C;
    border: none;
    border-radius: 3px;
    color: #FFF;
    cursor:pointer;
    display: inline-block;
    height: 30px;
    margin-right:15px;
    width: auto;
    padding:0 20px;
    box-sizing: content-box;
}

.fileName {
    font-family: Arial;
    font-size:14px;
}

.upload + .uploadButton {
    height:38px;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<form action="" method="post" enctype="multipart/form-data">
    <div class="upload">
        <input type="button" class="uploadButton" value="Browse" />
        <input type="file" name="upload" accept="image/*" id="fileUpload" />
        <span class="fileName">Select file..</span>
    </div>
    <input type="button" class="uploadButton" value="Upload File" />
</form>
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!!!

  • 一个巧妙巧妙的解决方案,这个邪恶的文件上传造型恶作剧. (2认同)

Tig*_*yan 9

将上传文件按钮放在漂亮的按钮或元素上,然后将其隐藏。

非常简单,可以在任何浏览器上使用

<div class="upload-wrap">
    <button type="button" class="nice-button">upload_file</button>
    <input type="file" name="file" class="upload-btn">
</div>
Run Code Online (Sandbox Code Playgroud)

款式

.upload-wrap {
    position: relative;
}

.upload-btn {
    position: absolute;
    left: 0;
    opacity: 0;
}
Run Code Online (Sandbox Code Playgroud)


Gia*_*ini 8

可见性:隐藏的TRICK

我经常去寻找visibility:hidden技巧

这是我的风格按钮

<div id="uploadbutton" class="btn btn-success btn-block">Upload</div>
Run Code Online (Sandbox Code Playgroud)

这是input type = file按钮.请注意visibility:hidden规则

<input type="file" id="upload" style="visibility:hidden;">
Run Code Online (Sandbox Code Playgroud)

这是将它们粘合在一起的JavaScript位.有用

<script>
 $('#uploadbutton').click(function(){
    $('input[type=file]').click();
 });
 </script>
Run Code Online (Sandbox Code Playgroud)


Sch*_*nge 8

具有转换文件名的多文件解决方案

引导示例

HTML:

<div>
  <label class="btn btn-primary search-file-btn">
    <input name="file1" type="file" style="display:None;"> <span>Choose file</span>
  </label>
  <span>No file selected</span>
</div>

<div>
  <label class="btn btn-primary search-file-btn">
    <input name="file2" type="file" style="display:None;"> <span>Choose file</span>
  </label>
  <span>No file selected</span>
</div>
Run Code Online (Sandbox Code Playgroud)

1. JS 与 jQuery:

$().ready(function($){
    $('.search-file-btn').children("input").bind('change', function() {
    var fileName = '';
    fileName = $(this).val().split("\\").slice(-1)[0];
    $(this).parent().next("span").html(fileName);
  })
});
Run Code Online (Sandbox Code Playgroud)

2. 没有 jQuery 的 JS

Array.prototype.forEach.call(document.getElementsByTagName('input'), function(item) {
  item.addEventListener("change", function() {
    var fileName = '';
    fileName = this.value.split("\\").slice(-1)[0];
    this.parentNode.nextElementSibling.innerHTML = fileName;
  });
});
Run Code Online (Sandbox Code Playgroud)


rom*_*n m 7

我能想到的唯一方法是在渲染后使用javascript找到按钮并为其指定样式

你也可以看一下这篇文章


use*_*641 7

<input type="file" name="media" style="display-none" onchange="document.media.submit()">
Run Code Online (Sandbox Code Playgroud)

我通常会使用简单的javascript来自定义文件输入标签.一个隐藏的输入字段,点击按钮,javascript调用隐藏字段,简单的解决方案,没有任何CSS或一堆jquery.

<button id="file" onclick="$('#file').click()">Upload File</button>
Run Code Online (Sandbox Code Playgroud)


小智 5

这是一个解决方案,它还显示了所选的文件名:http : //jsfiddle.net/raft9pg0/1/

HTML:

<label for="file-upload" class="custom-file-upload">Chose file</label>
<input id="file-upload" type="file"/>
File: <span id="file-upload-value">-</span>
Run Code Online (Sandbox Code Playgroud)

JS:

$(function() {
    $("input:file[id=file-upload]").change(function() {
        $("#file-upload-value").html( $(this).val() );
    });
});
Run Code Online (Sandbox Code Playgroud)

CSS:

input[type="file"] {
    display: none;
}

.custom-file-upload {
      background: #ddd;
      border: 1px solid #aaa;
      border-top: 1px solid #ccc;
      border-left: 1px solid #ccc;
      -moz-border-radius: 3px;
      -webkit-border-radius: 3px;
      border-radius: 3px;
      color: #444;
      display: inline-block;
      font-size: 11px;
      font-weight: bold;
      text-decoration: none;
      text-shadow: 0 1px rgba(255, 255, 255, .75);
      cursor: pointer;
      margin-bottom: 20px;
      line-height: normal;
      padding: 8px 10px; }
Run Code Online (Sandbox Code Playgroud)


rob*_*ing 5

这是使用材料/角度文件上传的好方法。你可以用引导按钮做同样的事情。

注意我使用<a>而不是<button>this 允许点击事件冒泡。

<label>
    <input type="file" (change)="setFile($event)" style="display:none" />

    <a mat-raised-button color="primary">
      <mat-icon>file_upload</mat-icon>
      Upload Document
    </a>

  </label>
Run Code Online (Sandbox Code Playgroud)


Abd*_*sha 5

在这里,我们使用一个span来触发文件类型的输入,并且我们只是自定义了span,因此我们可以使用这种方式添加任何样式。

请注意,我们将输入标签与“ visibility:hidden”选项一起使用,并在范围内触发它。

.attachFileSpan{
color:#2b6dad;
cursor:pointer;
}
.attachFileSpan:hover{
text-decoration: underline;
}
Run Code Online (Sandbox Code Playgroud)
<h3> Customized input of type file </h3>
<input id="myInput" type="file" style="visibility:hidden"/>

        <span title="attach file" class="attachFileSpan" onclick="document.getElementById('myInput').click()">
        Attach file
        </span>
Run Code Online (Sandbox Code Playgroud)

参考


Sto*_*ely 5

这是一个纯 CSS 跨浏览器解决方案!它是无 Javascript 的!这个也不会像这里的其他帖子一样尝试隐藏然后重新创建原始控件。它使用plain CSS没有任何马戏团技巧为所有主要浏览器设置原始文件上传表单控件的样式。

这是文件上传控件在 Firefox、Chrome 和 Edge 中使用以下 CSS 的样子。这是一个非常简单干净的设计。您可以将其更改为您喜欢的任何方式:

在此处输入图片说明

Internet Explorer 为您提供了有限的设计控制,但至少您可以使用 CSS 操作该控件,足以更改一些内容,包括圆角边框和颜色:

在此处输入图片说明

<style>
/* Note: This CSS will style all instances of 
   <input type=file /> controls in your website. */
input[type="file"],
input[type="file"]:visited,
input[type="file"]:hover,
input[type="file"]:focus,
input[type="file"]:active {
    margin:0;
    padding: 0em 0em;
    padding: 0rem 0rem;
    overflow: hidden; /* long file names overflow so just hide the end */
    background: #ffffff;
    border-radius: .2em;
    border-radius: .2rem;
    outline: none;
    border: 2px solid #bbb;
    cursor: pointer;
    -webkit-appearance: textfield;
    -moz-appearance: textfield;
}

input[type="file"]:hover {
    background: #f9f9ff; /* I am using a light blue to indicate an interaction */
    border: 2px solid #999;
}

input[type="file"]:visited,
input[type="file"]:focus,
input[type="file"]:active {
    background: #fff; /* Default back to white when focused. */
    border: 2px solid #999;
}

/* Note: Firefox flags the file name box as a *readonly* input. So that attribute selector was added below. Note: These selectors blow up in IE so have to be separated from the same styles above. */
input[type="file"]:disabled,
input[type="file"]:read-only {
    margin: 0;
    padding: 0em 0em;
    padding: 0rem 0rem;
    overflow: hidden; /* long file names overflow so just hide the end */
    background: #ffffff;
    border-radius: .2em;
    border-radius: .2rem;
    outline: none;
    border: 2px solid #bbb;
    cursor: pointer;
    -webkit-appearance: textfield;
    -moz-appearance: textfield;
}

input[type="file"]:disabled:hover,
input[type="file"]:read-only:hover {
    background: #f9f9ff; /* I am using a light blue to indicate an interaction */
    border: 2px solid #999;
}

input[type="file"]:disabled:visited,
input[type="file"]:disabled:focus,
input[type="file"]:disabled:active,
input[type="file"]:read-only:visited,
input[type="file"]:read-only:focus,
input[type="file"]:read-only:active {
    background: #fff; /* Default back to white when focused. */
    border: 2px solid #999;
}

/* IE UPLOAD BUTTON STYLE: This attempts to alter the file upload button style in IE.  Keep in mind IE gives you limited design control but at least you can customize its upload button.*/
::-ms-browse { /* IE */
    display: inline-block;
    margin: 0;
    padding: .2em .5em;
    padding: .2rem .5rem;
    text-align: center;
    outline: none;
    border: none;
    background: #fff;
    white-space: nowrap;
    cursor: pointer;
}
/* FIREFOX UPLOAD BUTTON STYLE */
::file-selector-button {/* firefox */
    display: inline-block;
    margin: 0rem 1rem 0rem 0rem;
    padding: .18em .5em;
    padding: .18rem .5rem;
    -webkit-appearance: button;
    text-align: center;
    border-radius: .1rem 0rem 0rem .1rem;
    outline: none;
    border: none;
    border-right: 2px solid #bbb;
    background: #eee;
    white-space: nowrap;
    cursor: pointer;
}
/* CHROME AND EDGE UPLOAD BUTTON STYLE */
::-webkit-file-upload-button { /* chrome and edge */
    display: inline-block;
    margin: 0rem 1rem 0rem 0rem;
    padding: .19em .5em;
    padding: .19rem .5rem;
    -webkit-appearance: button;
    text-align: center;
    border-radius: .1rem 0rem 0rem .1rem;
    outline: none;
    border: none;
    border-right: 2px solid #bbb;
    background: #eee;
    white-space: nowrap;
    cursor: pointer;
}
</style>
Run Code Online (Sandbox Code Playgroud)

注意:您可以在旧版本的 Firefox、Chrome、Edge 和 IE 中对其进行测试。


归档时间:

查看次数:

912728 次

最近记录:

6 年,1 月 前