简体   繁体   English

li:列表中特定班级的第一个孩子?

[英]li:first-child for specific class in list?

i have a list, which is like this: 我有一个列表,如下所示:

<ul>
  <li class="empty">bla</li>
  <li class="item">text1</li>
  <li class="item">text2</li>
<ul>

Is it now possible to select eg via JQuery the li:first-child of a specific class? 现在可以通过JQuery选择li:first-child特定类的li:first-child吗?

I don't want to select the class="empty" , i want to select the first item of "item"... 我不想选择class="empty" ,我想选择“item”的第一项......

$('ul li.item:first-child') is not working. $('ul li.item:first-child')无效。

Thanks! 谢谢!

$('ul li.item:first');

or 要么

$('ul li.item').first();

http://jsfiddle.net/ZMsdd/1/ http://jsfiddle.net/ZMsdd/1/

You can't use :first-child because it's defined as: "Selects all elements that are the first child of their parent." 你不能使用:first-child因为它被定义为: “选择所有父元素的第一个子元素。” and that is not true in your case. 在你的情况下,这是不正确的。 However, :first is defined as: "Selects the first matched element." 但是, :first定义为: “选择第一个匹配的元素”。 . And that works: 这有效:

$('ul li.item:first')

http://jsfiddle.net/n4JhH/ http://jsfiddle.net/n4JhH/

However, it actually does not work probably if there are multiple ul you want to apply this one. 但是,如果你想要应用多个ul ,它实际上可能不起作用。 Then you have to apply it for each with $('ul').each(...) <- I don't know whether there is an easier solution. 然后你必须为每个使用$('ul').each(...)应用它$('ul').each(...) < - 我不知道是否有一个更简单的解决方案。

Maybe this can be used 也许这可以用

$("li.item").first().hide();

jsfiddle here: http://jsfiddle.net/uPjP9/ jsfiddle: http//jsfiddle.net/uPjP9/

[edited] This should work [编辑]这应该工作

$('ul li:not(.empty):first')

http://jsfiddle.net/H9rxx/ http://jsfiddle.net/H9rxx/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM