概述 checkbox selector
返回值:Array<Element(s)>
描述:选择所有类型为复选框的元素。
$(':checkbox')
等同于 $('[type=checkbox]')
。如同其他伪类选择器(那些以“:”开始)建议前面加上一个标记名称或其他选择器;否则,默认使用通用选择("*")。换句话说$(':checkbox')
等同于 $('*:checkbox')
,所以应该使用$('input:checkbox')
来提升效率。
:checkbox
是一个 jQuery 延伸出来的选择器,并不是的CSS规范的一部分,使用:checkbox
查询不能充分利用原生DOM提供的querySelectorAll()
方法来提高性能。为了现代浏览器上获得更佳的性能,建议使用[type="checkbox"]
代替.
示例
查找所有复选框
<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>
$(":checkbox")
[ <input type="checkbox" /> ]
查找所有复选框
运行一下
- <!doctype html>
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <title>checkbox 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="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:checkbox" )
- .wrap( "<span></span>" )
- .parent()
- .css({
- background: "yellow",
- border: "3px red solid"
- });
- $( "div" )
- .text( "For this type jQuery found " + input.length + "." )
- .css( "color", "red" );
- // Prevent the form from submitting
- $( "form" ).submit(function( event ) {
- event.preventDefault();
- });
- </script>
- </body>
- </html>