:text 选择器
分类:选择器 > 表单选择器 | 选择器 > jQuery扩展选择器
text selector
描述:选择所有的type="text"的<input>元素。
加入于: 1.0
jQuery( ":text" )$( ":text" )允许我们选中所有的<input type="text">元素。与别的伪类选择器一样(它们以一个“:”开头),建议在它前面用一个标签名或者别的选择器,否则,会潜在地使用普遍选择器("*")。换句话说,光裸的$( ":text" )等同于$( "*:text" ),所以应该使用$( "input:text" )来代替它。
注意:自从jQuery 1.5.2,:text还会选择没有指定元素属性type的input元素(在这种情况下,会潜在地设定type="text")。
$( ":text" )和$( "[type=text]" )的行为区别,如下所示:
$( "<input>" ).is( "[type=text]" ); // false $( "<input>" ).is( ":text" ); // true
其它说明
- 因为
:text是一个jQuery扩展,并不是CSS规范文档的一部分,使用:text查询,不能充分利用原生DOM的querySelectorAll()方法来提高性能。要想在现代浏览器中获得更好的性能,请使用[type="text"]来代替它。
示例
查找所有的type="text"的<input>。
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>text demo</title>
<style>
textarea {
height: 25px;
}
</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 = $( "form input:text" ).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>
演示结果