简体   繁体   English

我正在尝试选择嵌套的ul,尝试使用CSS但遇到了麻烦

[英]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. 您提供的两个示例都依赖于查找.menu元素,但代码中均不存在。 it does now. 现在可以了。

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.CurrentButtondiv ,请使用以下命令:

.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.

相关问题 我在尝试拼接数组时遇到问题 - I'm having trouble trying to splice an array 无法识别我要单击“角度”页面的元素 - Having trouble identifying an element I'm trying to click on an Angular page 我在使用选择菜单更新状态时遇到问题 - I'm having trouble updating state with a select menu 我在使用模板时遇到麻烦 - I'm having trouble with templates 我正在尝试为越狱的iPhone制作iWidget。 我在处理HTML文件时遇到问题 - I'm trying to make an iWidget for my jailbroken iPhone. I'm having trouble with the HTML file 我正在尝试将对象键“品牌”及其值放入一个新数组中,但我遇到了麻烦 - I’m trying to get the object key “brand” and its value into a new array but I’m having trouble 如何在Javascript / css中的嵌套ul中选择最低级别的ul - How to select lowest level ul in nested ul in Javascript/ css 我试图用一个按钮随意移动一个CSS元素,我试图使用偏移和onlick不起作用 - I'm trying to move a CSS element at will with a button, I tried to use offset and onlick did not work 我做了一个Javascript滑块,而CSS遇到了一些严重的麻烦。 - I made a Javascript slider and i'm having some serious trouble with the CSS. 我想使用HTML中的JS和CSS做动画时遇到麻烦 - I'm having trouble with an animation I want to do with JS and CSS in HTML
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM