jQuery的層選擇器$("ancestor descendant")和$("parent>child")舉例說明
<div>This is <strong>very</strong> important.</div>
<div>This is <em>really <strong>very</strong></em> important.</div>
以上是html茫船。運行后在瀏覽器里是看到
1.JPG
這樣的樣式甩鳄,為了容易看到效果,我們不妨就嘗試用添加css顏色來試一下
如果運行
$("div strong").css("color","red");
就是把div的后代元素strong變?yōu)榧t色舞终。運行后是
2.JPG
如果運行
$("div>strong").css("color","blue");
就是把div的子元素strong變?yōu)樗{色。運行后是
3.JPG
- $("ancestor descendant")
表示所有的ancestor以下的descendant - $("parent>child")
表示當且僅當child緊跟著parent的時候