All Selector (“*”)

Description: Selects all elements.
描述:选择所有元素。
Caution: The all, or universal, selector is extremely slow, except when used by itself.
警示:这个选择器是非常缓慢的,除了自身使用时。

Examples:
Example: Finds every element (including head, body, etc) in the document.
例子:
查找文档中的所有元素(包括 head, body, 等等)。
<!DOCTYPE html>
<html>
<head>
  <style>
  h3 { margin: 0; }
  div,span,p {
    width: 80px;
    height: 40px;
    float:left;
    padding: 10px;
    margin: 10px;
    background-color: #EEEEEE;
  }
  </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <div>DIV</div>

  <span>SPAN</span>
  <p>P <button>Button</button></p>
<script>var elementCount = $("*").css("border","3px solid red").length;
$("body").prepend("<h3>" + elementCount + " elements found</h3>");</script>

</body>
</html>
Demo:

Example: A common way to select all elements is to find within document.body so elements like head, script, etc are left out.
例子:选择所有元素的一种常见方式是在document.body内查找,那么像head, script等等这些元素将被忽略。

<!DOCTYPE html>
<html>
<head>
  <style>
  h3 { margin: 0; }
  div,span,p {
    width: 80px;
    height: 40px;
    float:left;
    padding: 10px;
    margin: 10px;
    background-color: #EEEEEE;
  }
  #test {
    width: auto; height: auto; background-color: transparent;
  }
  </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <div id="test">
  <div>DIV</div>
  <span>SPAN</span>
  <p>P <button>Button</button></p>
</div>
<script>
var elementCount = $("#test").find("*").css("border","3px solid red").length;
$("body").prepend("<h3>" + elementCount + " elements found</h3>");</script>

</body>
</html>
Demo:

你可能感兴趣的:(jquery)