http://www.itfeed.cn/post.asp?id=3420
因为不同浏览器下如果使用默认的input type=”file”元素,效果如下
为了保证不同浏览器下的UI一致我们有需求要自定义input type=”file”元素的样式
如何Style input type=”file”元素的相关长篇讨论
1. Styling an input type=”file”
http://www.quirksmode.org/dom/inputfile.html
2. Howto Style the Button of a input type=”file” Control
http://imar.spaanjaars.com/154/howto-style-the-button-of-a-input-typefile-control
翻译成白话文就是
1. 隐藏input type=”file”
2. 用CSS将a+span做成按钮样式,对其做事件绑定,点击之后触发input type=”file”的点击事件
3. 用input type=”text”显示input type=”file”选中文件的值
查看能工作的live code
http://jsfiddle.net/jihao/sFCQV/
HTML
<input type="text" id="fakeFile" name="fakeFile">
<a class="linkButton" id="link_browse" href="#"><span>Link Button BROWSE</span></a>
<input id="theFile" type="file"
style="visibility:hidden;opacity:0;cursor:pointer;*filter: alpha(opacity=0);">
CSS
a.linkButton{
background: #332233;/*style this link to look like a button*/
color: #FFFFFF;
padding: 0 0 0 10px;
text-decoration: none;
}
a.linkButton>span{
padding: 0 10px 0 0;
}
JavaScript
$(function() {
$('#browse,#link_browse').click(function(event) {
event.preventDefault();
$('#theFile').trigger('click');
});
function changeCallBack() {
$('#fakeFile').val($('#theFile').val());
};
//this works for FF, easy job
$('#theFile').bind('change', function() {
alert('gotcha!');
changeCallBack();
});
});
写完这段代码之后遇到的相关问题
问题1. FireFox下不会显示文件全路径(google得知这是FF的安全考虑,不影响form提交)
问题2. IE下选择文件后不会触发change事件,原因是IE处理打开文件的机制(参考下面的链接看更多讨论)
对于IE8,这个问题的解决办法如下,setTimeout会阻塞,直到文件选择器关闭之后,回调函数才被调用(更新用来显示的input type=”text”的值)
if ($.browser.msie) {
$('#theFile').bind('click', function() {
//this solution works for IE8, not Maxthon2, so seems not IE6 either(not tested)
setTimeout(function() {
alert('[IE8] setTimeout get called after open file dialog was closed');
changeCallBack();
}, 0);
});
}
查看能工作的live code
http://jsfiddle.net/jihao/sFCQV/
关于IE change事件的相关讨论
1. jQuery, .change() notification, and IE (关于radio button的change事件绑定)
http://norman.walsh.name/2009/03/24/jQueryIE
2. jQuery: change event to input file on IE
http://stackoverflow.com/questions/2389341/jquery-change-event-to-input-file-on-ie
就是说IE不blur的话file不触发change事件,解决办法是设置一个setTimeout,然后再0毫秒后执行,利用文件选择框的阻塞。
分享到:
相关推荐
如何将input type=file显示的浏览变成英文的,简单实用!
input type=file 调取手机照相机和选择照片上传 附件下载demo导入运行即可
HTML input type=file文件选择表单元素.docx
input type=file 显示的浏览 改成英文
C#如何使用input[type=file]进行多个图片上传(XMLHttpRequest与后台交互) 详情可看:https://blog.csdn.net/weixin_44713389/article/details/90746459
input[type=file] 标签中的属性accept=application/msword,application/pdf 在pc上正常,但是在手机ios和android上这个文件格式限制会被忽略,所以需要在js中增加格式的判断,以及对应显示样式的设置.(我也是刚发现,...
解决在webview中input标签type="file"不能使用的问题
type=file选择图片后,在页面预览显示出来
input type="file">元素,它的默认样式: chrome下: IE下: 不管是上面哪种,样式都比较简单,和很多网页的风格都不太协调。 根据用户的需求,设计风格,改变其显示样式的场合就比较多了。 如果,要像下面一样...
利用jquery的工具ajaxfileupload.js配合input[type=file]实现无刷新上传文件,内含教程以及支持文件,希望可以帮助各位,谢谢!
怎样让input type=file 只读,并能实现文件上传
input type=”file” name=”file” /> 既然这样我们就用一个text和一个button来显示这个file的样式,html代码是这样: 复制代码代码如下: <div class=”file-box”> <form action=”” method=”post” ...
android webview input=file 失效解决方案
主要介绍了css input[type=file] 样式美化(input上传文件样式 )的相关资料,需要的朋友可以参考下
json struts2 ajax文件上传 input type=file,选择相同文件也提交,里面附带类库,可以直接运行。使用工具, myeclipse
通过网络搜索发现是因为 android webview 由于考虑安全原因屏蔽了 input type=”file” 这个功能 。 经过不懈的努力,以及google 翻译的帮助 在 stackoverflow 中找到了解决的方法。 具体可以理解为 重写webview 的...
自定义(滑动条)input[type="range"]样式; 完成以下的五个步骤: 去除系统默认的样式; 给滑动轨道(track)添加样式; 给滑块(thumb)添加样式; 根据滑块所在位置填充进度条; 实现多浏览器兼容。
input type=”file” name=”uploadfile” contentEditable=”false” style=”width:80%”>, 提交表单时需要判断其中至少要有一个input已经选择好文件。 代码如下: <input type=”file” name=”uploadfile...
对应博客:http://blog.csdn.net/u013347241/article/details/51560290