:button

选择所有按钮,包括 button 元素和 type 为 button 的元素。

$(":button") 等价的选择器可以使用有效的 CSS $("button, input[type='button']")

补充说明:

  • 由于 :button 是 jQuery 扩展出来的,它并不是 CSS 规范中的一部分。当使用 :button 时,并不会比使用原生的 DOM 方法 querySelectorAll() 性能好。为了达到高效的 :button 元素选择,请优先使用纯 CSS 选择器 .filter(":button")

示例:

查找所有的按钮并标记出它们。

<!DOCTYPE html>
<html>
<head>
<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="jquery.min.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 + "." );
  $("form").submit(function () { return false; }); // so it won't submit


</script>
</body>
</html>

演示: