概述 text selector
返回值:Array<Element(s)>
描述:选择所有类型为text的input元素。
$(':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()
方法来提高性能。为了当使用:text
的时候在现代浏览器上获得更佳的性能,首先使用纯CSS选择器选择元素,然后使用[type="text"]
代替.
示例
查找所有文本框
<form>
<input type="text" />
<input type="checkbox" />
<input type="radio" />
<input type="image" />
<input type="file" />
<input type="submit" />
<input type="reset" />
<input type="password" />
<input type="button" />
<select><option/></select>
<textarea></textarea>
<button></button>
</form>
$(":text")
[ <input type="text" /> ]
查找所有的文本框。
运行一下
- <!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" );
- // Prevent form submission
- $( "form" ).submit(function( event ) {
- event.preventDefault();
- });
- </script>
- </body>
- </html>