:file 选择器
分类:选择器 > 表单选择器 | 选择器 > jQuery扩展选择器
file selector
描述:选择所有的type="file"的<input>元素。
加入于: 1.0
jQuery( ":file" ):file is equivalent to [type="file"]. 与别的伪类选择器一样(它们以一个“:”开头),建议在它前面用一个标签名或者别的选择器,否则,会潜在地使用普遍选择器("*")。In other words, the bare $( ":file" ) is equivalent to $("*:file" ), so $( "input:file" ) should be used instead.
其它说明
- 因为
:file是一个jQuery扩展,并不是CSS规范文档的一部分,所以查询:file不能充分利用原生DOM提供的querySelectorAll()方法来提高性能。要想在现代浏览器中获得较好的性能,请使用[type="file"]来代替它。
示例
找到所有的提交文件的<input>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>file demo</title>
<style>
textarea {
height: 45px;
}
</style>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<form>
<input type="button" value="Input Button">
<input type="checkbox">
<input type="file">
<input type="hidden">
<input type="image">
<input type="password">
<input type="radio">
<input type="reset">
<input type="submit">
<input type="text">
<select><option>Option</option></select>
<textarea></textarea>
<button>Button</button>
</form>
<div></div>
<script>
var input = $( "input:file" ).css({
background: "yellow",
border: "3px red solid"
});
$( "div" )
.text( "For this type jQuery found " + input.length + "." )
.css( "color", "red" );
$( "form" ).submit(function( event ) {
event.preventDefault();
});
</script>
</body>
</html>
演示结果