作者tb6342 (acliu)
看板Ajax
标题[问题] Select first depth children
时间Tue Apr 19 19:53:33 2011
如下,假设有一个list下面还有一个list:
<ul>
<li>
<ul>
<li></li>
<li></li>
</ul>
<li>
<li></li>
<li></li>
</ul>
How can I use jQuery selector or HTML DOM to access the first depth <li> tags without those in the
sublist?
比方说我如果打$("ul li").method();则每个li都会执行这个method。
除了把第一层的li归类到同一个class外(ex.改成<li class="firstDepth">),
有没有其他更好的方法?
谢谢罗~
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 111.240.236.106
1F:推 s25g5d4:$("ul>li") 04/19 20:14
2F:→ grence:"ul li:eq(0)" 04/19 20:20
3F:→ grence:阿..一楼是对的 04/19 20:21
4F:→ terlin10:一楼这样不会有问题吗…觉得怪怪的 04/19 20:24
5F:推 s25g5d4:哪里有问题 这jQ documention就有了 04/19 20:25
7F:→ s25g5d4:而且这个 CSS就可以用了 只是似乎IE会挂掉 04/19 20:27
8F:→ terlin10:这个网站的ul有给class喔~如果照一楼的写法… 04/19 20:35
9F:→ terlin10:底下的ul li 应该也是会被影响 04/19 20:35
10F:→ terlin10:基本上还是建议原PO设个class吧!反正设class不用钱~XD 04/19 20:36
11F:→ s25g5d4:那就不是我的问题了阿 囧 我只是提出">" selector 04/19 20:36
12F:→ s25g5d4:在说你用其他方法还不是有可能取到第二个ul 04/19 20:36
13F:→ s25g5d4:我的话会把要取的ul加上id 变成$("#ID>li") 04/19 20:37
14F:→ terlin10:我只是针对原PO的问题来回应而已,况且…你在一楼的说法 04/19 20:37
15F:→ terlin10:以原PO的例子来讲,本来就是行不通的~ 04/19 20:38
16F:→ terlin10:所以应该是要建议原PO设个id或class就好了 04/19 20:38
17F:→ tb6342:恩我懂了谢谢!!因为我上面那两个list都是包在一个<div>里, 04/19 20:56
18F:→ tb6342:<div class="menu">,所以用$("div.menu>ul>li")就可以了~ 04/19 20:57
19F:→ mesak:$('ul:first > li') 04/20 10:16
20F:→ tb6342:恩!谢啦!! 04/20 11:40