如何根据状态内的链接更改listitem的颜色?

时间:2012-01-03 16:40:16

标签: javascript jquery asp.net css menu

问:

我有以下菜单:

如果active状态的链接,我想更改列表项的颜色。怎么做?

my.aspx:

        <div style="height: 50px">
            <ul id="topnav" style="text-align: center">
                <li><a href="MasterData.aspx">1111</a></li>
                <li><a href="ScheduleForm.aspx">2222 </a></li>
                <li><a href="MapingData.aspx">3333</a> </li>
                <li><a href="EditSchedule.aspx">4444</a> </li>
                <li><a href="LoginPage.aspx">5555</a></li>
            </ul>
        </div>

.css:

ul#topnav
{
    margin: 0px;
    padding: 2px 0px;
    clear: both;
    width: 980px;
    list-style: none;
    position: relative; /*--Set relative positioning on the unordered list itself - not on the list item--*/
    font-size: 1.2em;
    background-color: #CC6A11;
    height: 50px;
}
ul#topnav li
{
    float: right;
    margin: 0px auto; /* margin-right:0;*/
    padding: 5px 27px;
    border-right: 1px solid #555; /*--Divider for each parent level links--*/
    background-color: #CC6A11; /*url(topnav_stretch.gif) repeat-x;*/
    text-align:center;
}
ul#topnav li a
{
    padding: 10px 5px;
    display: block;
    color: #f0f0f0;
    text-decoration: none;
    width:121px;
    text-align:center;
}
ul#topnav li:hover
{
    background-color: #1376c9;
}

ul#topnav li a:active
{
    background-color: Purple;
}

ul#topnav li span
{
    text-align:right;
    float: right;
    padding: 5px 2px;
    position: absolute;
    margin-right: 0;
    left: 0;
    top: 52px;
    display: none; /*--Hide by default--*/
    width: 977px;
    z-index: 10;
    background-color: #1376c9;
    color: #fff; /*--Bottom right rounded corner--*/
    -moz-border-radius-bottomright: 5px;
    -khtml-border-radius-bottomright: 5px;
    -webkit-border-bottom-right-radius: 5px; /*--Bottom left rounded corner--*/
    -moz-border-radius-bottomleft: 5px;
    -khtml-border-radius-bottomleft: 5px;
    -webkit-border-bottom-left-radius: 5px;
}
ul#topnav li:hover span
{

}
/*--Show subnav on hover--*/
ul#topnav li span a
{
    display: inline;
}
/*--Since we declared a link style on the parent list link, we will correct it back to its original state--*/
ul#topnav li span a:hover
{
    text-decoration: underline;
}

.js:

$(document).ready(function() {

    $("ul#topnav li").hover(function() { //Hover over event on list item
        $(this).css({ 'background': '#1376c9  repeat-x' }); //Add background color and image on hovered list item
        $(this).find("span").show(); //Show the subnav
    }, function() { //on hover out...
        $(this).css({ 'background': '#CC6A11' }); //Ditch the background
        $(this).find("span").hide(); //Hide the subnav
    });

});

2 个答案:

答案 0 :(得分:0)

您需要以某种方式'标记'锚标记为活动标记,通常使用id:

<div style="height: 50px">
            <ul id="topnav" style="text-align: center">
                <li><a href="MasterData.aspx" id="active">1111</a></li>
                <li><a href="ScheduleForm.aspx">2222 </a></li>
                <li><a href="MapingData.aspx">3333</a> </li>
                <li><a href="EditSchedule.aspx">4444</a> </li>
                <li><a href="LoginPage.aspx">5555</a></li>
            </ul>
        </div>

通过这种方式,可以轻松生成与活动链接不同的CSS。您需要在代码中设置该ID(除非每个页面都有单独的文件)

同时查看http://hicksdesign.co.uk/journal/highlighting-current-page-with-css,因为在那里有一个很好的替代方法。

答案 1 :(得分:-1)

如果你需要一些非常简单的东西,下面的内容就是

See my fiddle - 为了让小提琴做你想做的事,你需要点击“运行”。这将导航浏览器“/ _display /”,然后您将看到突出显示“显示”链接。

<style>
    .highlight
    {
       background-color:yellow;
    }
</style>

<script>
    $(function(){
    var path = window.location.pathname;
     console.log(path);
    $("a[href='" + path +"']" ).addClass("highlight");
    });
</script>

<ul id="navmenu">
    <li><a href="guy.asp">me</a></li>
    <li><a href="/_display/">display</a></li>
</ul>

如果链接包含参数,那么您将需要一个循环。让我知道,我将在这里发布代码。

相关问题