如何仅使用 HTML 和 CSS(也许很小的 JavaScript)制作多层下拉菜单

发布于 2024-10-13 18:13:29 字数 1760 浏览 4 评论 0原文

CSS 新手,只能找到单级菜单。这是菜单和列表项:

<ul>
    <li>
        <a href="#">Home</a>
    </li>
    <li>
        <a href="#">Forums</a>
        <ul>
            <a href="#">Basketball</a>
            <ul>
                <li>
                    <a href="#">Trading</a>
                </li>
                <li>
                    <a href="#">Personal Collections</a>
                </li>
                <li>
                    <a href="#">Box Breaks</a>
                </li>
            </ul>
        </ul>
    </li>
</ul>

正如您所看到的,它将是多层的。现在,使用我拥有的 CSS,首先仅显示主页和论坛,当我将鼠标悬停在论坛上时,会显示篮球......但后续菜单项也是如此。我希望它们保持隐藏状态,直到我将鼠标悬停在篮球上。有人知道如何仅使用 CSS 或尽可能少的 JavaScript 来做到这一点吗?谢谢。这是我的 CSS 代码:

ul
{
    margin: 0;
    padding: 0;
    list-style: none;
    width: 150px;
    border-bottom: 1px solid #ccc;
}

ul li
{
    position: relative;
}

li ul
{
    position: absolute;
    left: 149px;
    top: 0;
    display: none;
}

ul li a
{
    display: block;
    text-decoration: none;
    color: #777;
    background: #bad8f8;
    padding: 2px 0 2px 10px;
    border: 1px solid #ccc;
    border-bottom: 0;
}

li:hover ul
{
    display: block;
}

/* Styles for validation helpers
-----------------------------------------------------------*/
.field-validation-error
{
    color: #ff0000;
}

.field-validation-valid
{
    display: none;
}

.input-validation-error
{
    border: 1px solid #ff0000;
    background-color: #ffeeee;
}

.validation-summary-errors
{
    font-weight: bold;
    color: #ff0000;
}

.validation-summary-valid
{
    display: none;
}

Newbie to CSS and can only find single level menus. Here's the Menu and the list items:

<ul>
    <li>
        <a href="#">Home</a>
    </li>
    <li>
        <a href="#">Forums</a>
        <ul>
            <a href="#">Basketball</a>
            <ul>
                <li>
                    <a href="#">Trading</a>
                </li>
                <li>
                    <a href="#">Personal Collections</a>
                </li>
                <li>
                    <a href="#">Box Breaks</a>
                </li>
            </ul>
        </ul>
    </li>
</ul>

As you can see, it would be multi-tiered. Now, with the CSS I have, only Home and Forums are displayed first, and when I hover over Forums, Basketball is displayed...but so are the subsequent menu items. I want those to stay hidden until I hover over basketball. Anyone know how to do this with just CSS or as little JavaScript as possible? Thanks. Here's the CSS code I have:

ul
{
    margin: 0;
    padding: 0;
    list-style: none;
    width: 150px;
    border-bottom: 1px solid #ccc;
}

ul li
{
    position: relative;
}

li ul
{
    position: absolute;
    left: 149px;
    top: 0;
    display: none;
}

ul li a
{
    display: block;
    text-decoration: none;
    color: #777;
    background: #bad8f8;
    padding: 2px 0 2px 10px;
    border: 1px solid #ccc;
    border-bottom: 0;
}

li:hover ul
{
    display: block;
}

/* Styles for validation helpers
-----------------------------------------------------------*/
.field-validation-error
{
    color: #ff0000;
}

.field-validation-valid
{
    display: none;
}

.input-validation-error
{
    border: 1px solid #ff0000;
    background-color: #ffeeee;
}

.validation-summary-errors
{
    font-weight: bold;
    color: #ff0000;
}

.validation-summary-valid
{
    display: none;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

静谧幽蓝 2024-10-20 18:13:29

这是一个工作演示:

http://jsfiddle.net/rcravens/aqz8q/

我做了两件事。

  1. 对 ul/li 列表进行了一些重组。有些元素不在 li 中。

  2. 使用 'li:hover > ul' 仅选择直接子级。

希望有帮助。

鲍勃

Here is a working demo:

http://jsfiddle.net/rcravens/aqz8q/

Two things I did.

  1. A bit of restructuring the ul/li list. There were some elements not in the li.

  2. Used 'li:hover > ul' to select the direct children only.

Hope that helps.

Bob

老娘不死你永远是小三 2024-10-20 18:13:29

尝试添加

ul li ul{position: absolute; top: 0; left: 0; width: 250px; height: 250px; background-color: #EEE;}

并从那里开始:)

Try to add

ul li ul{position: absolute; top: 0; left: 0; width: 250px; height: 250px; background-color: #EEE;}

And go from there :)

墟烟 2024-10-20 18:13:29

您构建 css 的方式会导致“篮球”的所有后代继承其 css。您应该使用 child(">") 或 :first-child 选择器。请参阅第 5.5 节和第 5.6 节,了解我在说什么:http://www.w3.org/TR/CSS2/selector.html w3.org/TR/CSS2/selector.html

如果您正在寻找动态菜单,我强烈建议您使用 javascript 而不是纯粹依赖 css,除非您确定很多人会访问您的网站他们的 javascript 已关闭。

The way you have structured your css causes all of "Basketball"'s descendants to inherit its css. You should be using a child(">") or :first-child selector instead. Look at sections 5.5 and 5.6 here to know what I am talking about: http://www.w3.org/TR/CSS2/selector.html

If you are looking to do dynamic menus I'd strongly recommend using javascript over relying purely on css, unless you are certain a lot of people viewing your website are going to have their javascript turned off.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文