在列表中选择第一个嵌套子级,但不选择子级 [英] Select first nested child in list but not subchilds

查看:86
本文介绍了在列表中选择第一个嵌套子级,但不选择子级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个无序的列表,如:

I have a unordered list like:

<ul>
    <li>Element one
        <ul>
            <li><a>Element two</a>
                <ul>
                    <li><a>Element three</a></li>
                </ul>
            </li>
        </ul>
    </li>
</ul>

现在,如果我尝试选择< a> - 元素二的元素。我会做如下:

Now if I try to select the <a>-element of "Element two". I would do it like the following:

ul ul li:first-child > a

这将同时选择以下嵌套< a> 元素。看到这个小提琴:

This will select also the following nested <a> element. See this fiddle:

http://jsfiddle.net/38ksdpw3 /

如何解决这个问题?

推荐答案

您可能需要向第一个< ul> 元素添加标识符,然后通过 parent>子 子选择器,如下所示:

You probably need to add an identifier to the first <ul> element and then walk through the children tree by parent > child children selector as follows:

示例

Example Here

ul.main > li > ul > li:first-child > a {
    background:red;
}

这篇关于在列表中选择第一个嵌套子级,但不选择子级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆