全选中选择器 (“*”)
all selector
示例
找到document中的每个元素(包括head、body,等等)。注意,如果你的浏览器具有一个扩展/附加项,启用了向DOM插入一个<script>
或者<link>
元素,这个元素也会被点数进去。
<!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-1.10.2.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内部的所有元素,从而一些元素,比如说head、script等等被排除在外。
<!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-1.10.2.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>
演示结果