概述 button selector
返回值:Array<Element(s)>
描述:选择所有按钮元素和类型为按钮的元素。
一个和$(":button")等价的选择器,使用有效的CSS是$("button, input[type='button']")。
:button 是一个 jQuery 延伸出来的选择器,并不是的CSS规范的一部分,使用:button 查询不能充分利用原生DOM提供的querySelectorAll() 方法来提高性能。为了当使用:button 的时候在现代浏览器上获得更佳的性能,首先使用纯CSS选择器选择元素,然后使用.filter(":button").
示例
查找所有按钮.
<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>
$(":button")
[ <input type="button" />,<button></button> ]
查找所有的按钮并标记出它们。
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>button demo</title>
<style>
textarea {
height: 35px;
}
div {
color: red;
}
fieldset {
margin: 0;
padding: 0;
border-width: 0;
}
.marked {
background-color: yellow;
border: 3px red solid;
}
</style>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<form>
<fieldset>
<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>
</fieldset>
</form>
<div></div>
<script>
var input = $( ":button" ).addClass( "marked" );
$( "div" ).text( "For this type jQuery found " + input.length + "." );
// Prevent the form from submitting
$( "form" ).submit(function( event ) {
event.preventDefault();
});
</script>
</body>
</html>
运行一下