将ul的边框颜色更改为兄弟姐妹" a"背景颜色

时间:2014-09-11 14:22:26

标签: javascript jquery css html5

我正在创建一个嵌套在子菜单中的菜单。每个ancor标签都有不同的背景颜色。 我想要发生的是,每个嵌套的ul的底部边框颜色与该特定组“li”中的ancor标签的背景颜色相同。

我看起来只有一半,但现在所有嵌套的ul都从第一个ancor标签背景颜色获得相同的边框颜色。不知道我哪里出错了。

<nav id="nav-main">
    <ul>
        <li><a href="">Spa Menu</a>
            <ul>
                <li><a href="">Packages</a></li>
                <li><a href="">Touch Therapy</a></li>
                <li><a href="">Hands & Feet</a></li>
                <li><a href="">Grooming</a></li>
                <li><a href="">Specialised Treatments</a></li>
            </ul>
        </li>
        <li><a href="">Bookings</a>
            <ul>
                <li><a href="">Packages</a></li>
                <li><a href="">Touch Therapy</a></li>
                <li><a href="">Hands & Feet</a></li>
                <li><a href="">Grooming</a></li>
                <li><a href="">Specialised Treatments</a></li>
            </ul>
        </li>
        <li><a href="">Vouchers</a></li>
        <li><a href="">Gallery</a></li>
    </ul>
</nav><!-- END #nav-main -->

css

#nav-main {
    float: right;
    position: relative;
    top: 8em;
}
#nav-main > ul > li {
    float: left;
    position: relative;
}
#nav-main > ul > li a {
    color: white;
    text-transform: uppercase;
    font-family: 'Brandon Grotesque', sans-serif;
    font-weight: bold;
    font-size: 18px;
    padding-left: 2em;
    padding-right: 2em;
}
#nav-main > ul > li ul {
    position: absolute;
    top: 100%;
    left: 0;
    z-index: 2;
    border-bottom: 8px solid ;
}
#nav-main > ul > li ul li a {
    white-space: nowrap;
    text-align: left;
    background: #fff;
    color: #b0b0b0;
    font-size: 16px;
}
#nav-main > ul > li:nth-child(1) > a {
    background-color: #5ac6eb;
}
#nav-main > ul > li:nth-child(2) > a {
    background-color: #bcd34d;
}
#nav-main > ul > li:nth-child(3) > a {
    background-color: #5ac6eb;
}
#nav-main > ul > li:nth-child(4) > a {
    background-color: #5ac6eb;
}

和js让它工作

jQuery(document).ready(function($){
    'use strict';

    var child = $('#nav-main ul >li >ul');

    child.css({
        'border-color': child.siblings('a').css('background-color')
    });
});

1 个答案:

答案 0 :(得分:3)

试试这个:

$('#nav-main ul > li > ul').each(function() {
   $(this).css('border-color', $(this).prev('a').css('background-color'));
});

http://jsfiddle.net/zneapo43/

我必须诚实地说,但它看起来非常混乱。

相关问题