當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


JQuery All Selector (“*”)用法及代碼示例

用法
all selector

說明:選擇所有元素。

  • 添加的版本:1.0jQuery( "*" )

警告:所有或通用選擇器非常慢,除非單獨使用。

例子:

查找文檔中的每個元素(包括頭部、主體等)。請注意,如果您的瀏覽器啟用了將 <script><link> 元素插入 DOM 的擴展程序/add-on,則該元素也將被計算在內。

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>all demo</title>
  <style>
  h3 {
    margin: 0;
  }
  div, span, p {
    width: 80px;
    height: 40px;
    float: left;
    padding: 10px;
    margin: 10px;
    background-color: #EEEEEE;
  }
  </style>
  <script src="https://code.jquery.com/jquery-3.5.0.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>

演示:

查找document.body 中的所有元素,以便排除頭部、腳本等元素。

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>all demo</title>
  <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="https://code.jquery.com/jquery-3.5.0.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>

演示:

相關用法


注:本文由純淨天空篩選整理自jquery.com大神的英文原創作品 All Selector (“*”)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。