[英]I'm trying to select a nested ul, tried with css but having trouble
I have a cms rendering the menus for a website I am designing and I need to select the children of the parent menu items. 我有一个cms呈现我正在设计的网站的菜单,我需要选择父菜单项的子项。 Here is how the code is generated:
这是代码的生成方式:
<div class="menu">
<ul>
<li></li>
<li><a href="" class="CurrentButton" ... />text</a>
<div>
<ul></ul>
</div>
</li>
</ul>
</div>
I have tried this CSS to try to select it but it's been unsuccessful as of yet aside from the display :none; 我已经尝试过这个CSS来尝试选择它,但是除了display:none;之外,它都没有成功。
.menu ul li div {
display: none;
}
.menu > ul > li > a.CurrentButton div {
display: block;
}
can anyone see what I'm doing wrong? 谁能看到我在做什么错? Would a jquery function be easier?
jQuery函数会更容易吗? I'm fairly new to jquery so I'm not sure how to go about writing a function for it.
我对jquery还是很陌生,所以我不确定如何为它编写函数。
I am trying to select the div within the li when the anchor within that li has the class CurrentButton, if the anchor within the li doesn't have the class then I want it hidden 我试图在li内的锚点具有CurrentButton类时选择li内的div,如果li内的锚点不具有该类,那么我希望将其隐藏
If you really need to be specific, use the adjacent elements operator ( + ) 如果确实需要具体说明,请使用相邻的元素运算符(+)
.menu > ul > li > a.CurrentButton + div {
Otherwise, you are targeting a div that is the descendent of CurrentButton
and that doesn't exist. 否则,您将定位作为
CurrentButton
的后代并且不存在的div。
If you don't need to be so specific, use the same selector as before: 如果您不需要那么具体,请使用与以前相同的选择器:
.menu > ul > li > div {
Both of the examples you give rely on finding the .menu
element, but none exists in your code.您提供的两个示例都依赖于查找 it does now. .menu
元素,但代码中均不存在。现在可以了。
a.CurrentButton div
selects any divs
inside of any a.CurrentButton
s. a.CurrentButton div
选择任何a.CurrentButton
内的任何divs
。 However, your divs are not inside the a
s. 但是,您的div不在
a
s内。 Try this: 尝试这个:
.menu ul > li > a {
//selects all the as, but non of the divs
}
.menu ul > li > * {
//selects anything inside a 'li', both 'a's and 'div's
}
To select div
s that follow a.CurrentButton
s, use this: 要选择
a.CurrentButton
的div
,请使用以下命令:
.menu ul li > a.CurrentButton + div {
//any 'div's that are directly after 'a.CurrentButton'
}
Assuming the <ul>
above is a child of an element with the class .menu
, the <div>
above is not a child of a.CurrentButton
, so you should select it like this: 假设上面的
<ul>
是.menu
类的元素的子元素,那么上面的<div>
不是a.CurrentButton
,因此您应该这样选择:
.menu > ul > li > div {
display: block;
}
Just so you know >
only selects direct children of an element. 众所周知,
>
仅选择元素的直接子代 。
Try this: 尝试这个:
In your HTML div is a sibling of a.CurrentButton. 在您的HTML div中是a.CurrentButton的同级对象。 So, you should use
+
sign. 因此,您应该使用
+
号。
.menu ul li div {
display: none;
}
.menu > ul > li > a.CurrentButton + div {
display: block;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.