input file的样式不能直接用css来美化,咱们能够曲线救国,把input file的透明度下降为0,至关于把这个控件隐藏了,实际上只是透明度为0,仍是存在的,而后把div套上去,让div充当file的按钮。css
因此这个办法很简单,可是,把input file的透明度下降,连选择文件后的文件名也被隐藏了,这下可怎么办?那就只能用jquery获取file的文件名了。html
<!DOCTYPE html> <html> <head> <title>文件上传file美化</title> <style type="text/css"> *{margin:0;padding: 0;} #btn{ width: 120px; height: 45px; background: #39f; line-height: 45px; text-align: center; color: #fff; } #btn .file{ opacity: 0; position: relative; top: -56px; width: 100%; height: 45px; } </style> </head> <body> <!-- 用一个div来看成美化的上传按钮,file按钮被透明化 --> <div id="btn"> 文件上传 <input type="file" class="file" onclick="daojishi()"> </div> <!-- 文件名显示区域 --> <div id="filename"></div> <script src="https://www.jq22.com/jquery/jquery-3.3.1.js"></script> <!-- 轮询文件名 --> <script> function daojishi() { setInterval("getname()",1000); } </script> <!-- 获取文件名 --> <script> function getname() { var filename = $("#btn .file").val(); $("#filename").text(filename); } </script> </body> </html>
搞定!jquery
做者:TANKING
网站:http://www.likeyunba.com网站