【发布时间】:2010-11-16 03:02:35
【问题描述】:
【问题讨论】:
标签: jquery css jquery-selectors css-selectors
【问题讨论】:
标签: jquery css jquery-selectors css-selectors
为:
<ul>
<li>Item 1</li>
<li>Item 2
<ul>
<li>Item 2.1</li>
<li>Item 2.2</li>
</ul>
</li>
<li>Item 3</li>
</ul>
例如
$("ul > li").addClass("blah");
将“blah”类添加到 1 2 和 3 而:
$("ul li").addClass("blah");
将类“blah”添加到每个列表元素。
我不确定你用
【讨论】:
ul 设置为 ol,因为您当前的代码不会像您描述的那样运行(尽管在技术上是正确的)。
在 CSS 中,> 表示“直接子级”:仅选择直接子级的节点。
虽然空格表示“任何后代”:可以选择直接子代和这些子代的子代。
我敢打赌 jQuery 使用相同的约定。
【讨论】:
如前所述,空格将选择任何后代,而> 将仅选择直接子代。如果你只想选择孙子或曾孙,那么你可以使用这个:
#foo > * > * > .bar
(所有具有“bar”类的元素,它们是 id 为“foo”的元素的曾孙)
【讨论】:
看看这个..
$(".testit > a") //match the first <a> tag below
$(".testit a") // matches all <a> tag below
<p class="testit">
<a href="#">All the rules will match this</a>
<span>
<a href="#">second rule can only select this</a>
</span>
</p>
【讨论】: