Select first nested child in list but not subchilds

蓝咒 提交于 2020-01-14 08:48:30

问题


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>

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

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

http://jsfiddle.net/38ksdpw3/

How can this be solved?


回答1:


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;
}



回答2:


Actually you can not diffirintiate it in such structure. For both links you have ul ul li:first-child > a applied without the specific parent. So you should consider to use classes (at least for parenting) instead of tags.



来源:https://stackoverflow.com/questions/25887234/select-first-nested-child-in-list-but-not-subchilds

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!