:password

选择所有类型为密码的元素。

jQuery(":password")

$(':password') 等价于 $('[type=password]') 。如同其他伪类选择器(那些以“:”开始)建议前面加上一个标记名称或其他选择器;否则,通用选择("*")被默认使用。换句话说 $(':password') 等同于 $('*:password') ,所以应该使用 $('input:password')

注意

  • 因为 :password 是一个 jQuery 延伸出来的选择器,并不是的CSS规范的一部分,使用 :password 查询不能充分利用原生DOM提供的 querySelectorAll() 方法来提高性能。为了当使用 :password 的时候在现代浏览器上获得更佳的性能,首先使用纯CSS选择器选择元素,然后使用 [type="password"] 代替.

例子

Finds all password inputs.

<!DOCTYPE html>
<html>
<head>
  <style>
  textarea { height:45px; }
  </style>
  <script src="./static/js/jquery-3.5.0.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 = $("input:password").css({background:"yellow", border:"3px red solid"});
$("div").text("For this type jQuery found " + input.length + ".")
        .css("color", "red");
$("form").submit(function () { return false; }); // so it won't submit
</script>
 
</body>
</html>

上篇: :input

下篇: :radio