标签:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>无标题文档</title> <script src="jquery-3.0.0.min.js"></script> <style> div { width: 100%; height: 20px; text-align:center; border: 1px solid #555; margin: 5px; } </style> </head> <body> <div id="a"></div> <div id="a"></div> <script> $("#a").html("我选中你了"); </script> </body> </html>
执行结果:只选中第一个。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>无标题文档</title> <script src="jquery-3.0.0.min.js"></script> <style> div { width: 100%; height: 20px; text-align:center; border: 1px solid #555; margin: 5px; } </style> </head> <body> <div class="a"></div> <div class="a"></div> <script> $(".a").html("我选中你了"); </script> </body> </html>
执行结果:两个div都会被选中。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>无标题文档</title> <script src="jquery-3.0.0.min.js"></script> <style> div, span { width: 100%; height: 20px; text-align:center; border: 1px solid #555; margin: 5px; display:block; } </style> </head> <body> <div></div> <div></div> <span></span> <span></span> <script> $("span").html("我选中你了"); </script> </body> </html>
执行结果:两个span元素会被选中。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>无标题文档</title> <script src="jquery-3.0.0.min.js"></script> <style> div, span { width: 100%; min-height: 20px; text-align:center; border: 1px solid #555; margin: 5px; display:block; } </style> </head> <body> <div><span></span></div> <div><div></div></div> <span></span> <span></span> <script> $("body *").css("border-color","red"); </script> </body> </html>
执行结果:body下的所有子元素(包含子元素的子元素)都被选中。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>无标题文档</title> <script src="jquery-3.0.0.min.js"></script> <style> div, span { width: 100%; min-height: 20px; text-align:center; border: 1px solid #555; margin: 5px; display:block; } </style> </head> <body> <div id="a"></div> <div class="b"></div> <span> <div></div> <div></div> </span> <div></div>
<script> $("#a,.b,span *").html("我选中你了"); </script> </body> </html>
上例中,$("#a,.b,span *")包含了三个选择器,#a选中id等于a的元素,.b选中class等于b的元素,span *选中所有span下的所有元素。所以执行结果为:前两个div以及span内的两个div会被选中。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>无标题文档</title> <script src="jquery-3.0.0.min.js"></script> <style> div, span { width: 100%; min-height: 20px; text-align:center; border: 1px solid #555; margin: 5px; display:block; } </style> </head> <body> <div class="a"></div> <span class="a"></span> <span id="b"></span> <span id="c"> <a></a> <a></a> </span> <script> $("div.a, span#b, span#c *").html("我选中你了"); </script> </body> </html>
执行结果:class等于a的div将会被选中,id等于b的span将会被选中,id等于c的所有子元素将会被选中。
选择“选择器1”的子元素(包含子元素的子元素)中,匹配“选择器2”的所有元素
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>无标题文档</title> <script src="jquery-3.0.0.min.js"></script> <style> div, span { width: 100%; min-height: 20px; text-align:center; border: 1px solid #555; margin: 5px; display:block; } </style> </head> <body> <a>第1个</a> <span> <div> <a>第2个</a> <a>第3个</a> </div> <a>第4个</a> <div> <a>第5个</a> <span> <a>第6个</a> </span> </div> </span> <script> $("span a").css("color","red"); </script> </body> </html>
执行结果:2-6会被选中。
选择“选择器1”的子元素(必须是直属父子元素)中,匹配“选择器2”的所有元素
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>无标题文档</title> <script src="jquery-3.0.0.min.js"></script> <style> div, span { width: 100%; min-height: 20px; text-align:center; border: 1px solid #555; margin: 5px; display:block; } </style> </head> <body> <a>第1个</a> <span> <div> <a>第2个</a> <a>第3个</a> </div> <a>第4个</a> <div> <a>第5个</a> <span> <a>第6个</a> </span> </div> </span> <script> $("span>a").css("color","red"); </script> </body> </html>
未完待续。。。
标签:
原文地址:http://www.cnblogs.com/LOVE0612/p/5608839.html