在进行文件上传得过程中,通常需要用到input file 标签,但是由于其默认的表单元素丑陋的外观,今天我在做上传的时候,也遇到了这个问题,真心太丑,和周围的其他表单元素,完全是格格不入啊。于是就想深入学习了解下,然后找到了一个比较简单的方法。其实很简单,就是通过label标签与input标签绑定,隐藏input标签,给label标签或者label里面的标签的设置样式:
<form action="" method="" enctype='multipart/form-data'>
<label for='my_file' class='inputlabelBox'>
<p class="inputBox">点击上传文件</p>
</label>
<input type="file" name="myfile" id='my_file' style="display:none;" />
</form>
<img src="" id="img" width="300" />在通过Js上传图片预览后:
var inputlabelBox = document.querySelector('.inputlabelBox');
var my_file = document.querySelector('#my_file');
var img = document.querySelector('#img');
my_file.onchange = function()
{
var file = this.files[0];
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function()
{
img.src = this.result;
}
}相关推荐:
以上就是js修改input file标签样式的方法的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号