【发布时间】:2012-11-12 08:58:46
【问题描述】:
W3C 验证器说:“此时元素输入上不允许使用属性大小。”
<input type="file" name="foo" size="40" />
在 HTML5 中指定文件输入宽度的正确方法是什么?
【问题讨论】:
W3C 验证器说:“此时元素输入上不允许使用属性大小。”
<input type="file" name="foo" size="40" />
在 HTML5 中指定文件输入宽度的正确方法是什么?
【问题讨论】:
在输入字段中使用 style 属性,这允许您使用 css。
<input type="file" name="foo" style="width: 40px" />
或者使用单独的 css:
input[name="foo"] {
width: 40px;
}
【讨论】:
在您的 css 文件中,指定宽度:
input[type=file] { width: 300px; border: 2px solid red; }
【讨论】:
<form class="upload-form">
<input class="upload-file" data-max-size="2048" type="file" >
<input type=submit>
</form>
<script>
$(function(){
var fileInput = $('.upload-file');
var maxSize = fileInput.data('max-size');
$('.upload-form').submit(function(e){
if(fileInput.get(0).files.length){
var fileSize = fileInput.get(0).files[0].size; // in bytes
if(fileSize>maxSize){
alert('file size is more then' + maxSize + ' bytes');
return false;
}else{
alert('file size is correct- '+fileSize+' bytes');
}
}else{
alert('choose file, please');
return false;
}
});
});
</script>
【讨论】: