使用Jquery的Active Ul li

时间:2013-03-19 07:45:38

标签: jquery html css ajax

我正在尝试将菜单设置为有效选项。当我使用jquery添加活动类时它不起作用。我在下面添加了所有的css和jquery代码。任何人都可以帮我解决这个问题。

 <link rel="shortcut icon" href="images/favicon.ico" type="image/x-icon" />
    <script src="js/modernizr-2.0.6.min.js" type="text/javascript"></script> 
<style type="text/css">

* {
    margin: 0;
    padding: 0;
    outline: none;  
}

ul {
    list-style: none;   
}

.navigation {
    margin: 0px 0;  
    font: bold 12px/18px "Helvetica Neue", Helvetica, Arial, sans-serif;
    text-transform: uppercase;
    color: #444;
    width:100%;
    -webkit-box-shadow: 0 1px rgba(255,255,255,1) inset;
    background: #F7F7F7; /* Old browsers */
    background: -moz-linear-gradient(top, #F7F7F7 0%, #EDEDED 100%); /* FF3.6+ */
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#F7F7F7), color-stop(100%,#EDEDED)); /* Chrome,Safari4+ */
    background: -webkit-linear-gradient(top, #F7F7F7 0%,#EDEDED 100%); /* Chrome10+,Safari5.1+ */
    background: -o-linear-gradient(top, #F7F7F7 0%,#EDEDED 100%); /* Opera 11.10+ */
    background: -ms-linear-gradient(top, #F7F7F7 0%,#EDEDED 100%); /* IE10+ */
    background: linear-gradient(top, #F7F7F7 0%,#EDEDED 100%); /* W3C */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#F7F7F7', endColorstr='#EDEDED',GradientType=0 ); /* IE6-9 */    
}

.navigation:after {
    clear: both;
    content: ".";
    display: block;
    height: 0;
    visibility: hidden;
}

.navigation ul {    
    float: left;
    border-radius: 0px;
    box-shadow: 0 2px 2px rgba(0, 0, 0, 0.07);
    -webkit-box-shadow: 0 2px 2px rgba(0, 0, 0, 0.07);
    overflow: hidden;
}

.navigation li {
    float: left;
    border-style: solid; 
    border-width: 1px;
    border-color: #BABABA #BABABA #BABABA #FFF;
    box-shadow: 0 1px rgba(255,255,255,1) inset;
    -webkit-box-shadow: 0 1px rgba(255,255,255,1) inset;
    background: #F7F7F7; /* Old browsers */
    background: -moz-linear-gradient(top, #F7F7F7 0%, #EDEDED 100%); /* FF3.6+ */
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#F7F7F7), color-stop(100%,#EDEDED)); /* Chrome,Safari4+ */
    background: -webkit-linear-gradient(top, #F7F7F7 0%,#EDEDED 100%); /* Chrome10+,Safari5.1+ */
    background: -o-linear-gradient(top, #F7F7F7 0%,#EDEDED 100%); /* Opera 11.10+ */
    background: -ms-linear-gradient(top, #F7F7F7 0%,#EDEDED 100%); /* IE10+ */
    background: linear-gradient(top, #F7F7F7 0%,#EDEDED 100%); /* W3C */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#F7F7F7', endColorstr='#EDEDED',GradientType=0 ); /* IE6-9 */    
}

.navigation li:hover, navigation li.current {
    box-shadow: 0 1px rgba(255,255,255,0.2) inset;
    -webkit-box-shadow: 0 1px rgba(255,255,255,0.2) inset;
    border-color: #262626 !important;
    background: #4D4D4D; /* Old browsers */
    background: -moz-linear-gradient(top, #4D4D4D 0%, #262626 100%); /* FF3.6+ */
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#4D4D4D), color-stop(100%,#262626)); /* Chrome,Safari4+ */
    background: -webkit-linear-gradient(top, #4D4D4D 0%,#262626 100%); /* Chrome10+,Safari5.1+ */
    background: -o-linear-gradient(top, #4D4D4D 0%,#262626 100%); /* Opera 11.10+ */
    background: -ms-linear-gradient(top, #4D4D4D 0%,#262626 100%); /* IE10+ */
    background: linear-gradient(top, #4D4D4D 0%,#262626 100%); /* W3C */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#4D4D4D', endColorstr='#262626',GradientType=0 ); /* IE6-9 */
}


.navigation a {
    display: block;
    padding: 10px 15px;
    color: #444;
    text-decoration: none;
    text-shadow: 0 1px #FFF;
}
.navigation  a.active{
    box-shadow: 0 1px rgba(255,255,255,0.2) inset;
    -webkit-box-shadow: 0 1px rgba(255,255,255,0.2) inset;
    border-color: #262626 !important;
    background: #4D4D4D; /* Old browsers */
    background: -moz-linear-gradient(top, #4D4D4D 0%, #262626 100%); /* FF3.6+ */
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#4D4D4D), color-stop(100%,#262626)); /* Chrome,Safari4+ */
    background: -webkit-linear-gradient(top, #4D4D4D 0%,#262626 100%); /* Chrome10+,Safari5.1+ */
    background: -o-linear-gradient(top, #4D4D4D 0%,#262626 100%); /* Opera 11.10+ */
    background: -ms-linear-gradient(top, #4D4D4D 0%,#262626 100%); /* IE10+ */
    background: linear-gradient(top, #4D4D4D 0%,#262626 100%); /* W3C */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#4D4D4D', endColorstr='#262626',GradientType=0 ); /* IE6-9 */
    color:#fff;
}

.navigation a:hover, #navigation li.current a {
    color: #FFF;
    text-shadow: 0 1px #000;
}

.navigation li:first-child {
    border-left-color: #BABABA;
    border-radius: 0px 0 0 0px;
}

.navigation li:last-child {
    border-radius: 0 0px 0px 0;
}


</style>
<script type="text/javascript"> 

$(document).ready(function() {  


    $(".navigation li").click(function() {  
        $(".navigation li").removeClass("a.active"); //Remove any "active" class  
        $(this).addClass("a.active"); //Add "active" class to selected tab  


        $(activeTab).show(); //Fade in the active content  
        return false;  
    });  

});  
</script> 
<div id="banner">
<div id="topnavbar">
<div id="topnavmenu">
<a rel="nofollow" href="http://www.checkmytrip.com/">CheckmyTrip</a> | <a rel="nofollow" href="baggage.php">Baggage Allowance</a>| <a rel="nofollow" href="traveltips.php">Travel Tips</a>
</div>
</div>
<img src="images/Banner.png"  width="1000" height="150"/>

</div>



    <nav class="navigation">
        <ul>
            <li><a href="index.php" class="active" >Home</a></li>
            <li><a  href="cheap-flights-to-colombo-sri-lanka.php">Flights</a></li>
            <li><a href="destination.php">Travel Guide</a></li>
            <li><a href="holidaytours.php">Tours & Holidays</a></li>
            <li><a href="aboutus.php">About us</a></li>
            <li><a href="contactus.php">Contact Us</a></li>
        </ul>
    </nav>

3 个答案:

答案 0 :(得分:0)

您需要做的就是在代码中添加一个jQuery。

您忘记加载jQuery.js文件。

您正在调用LI click上的脚本并将类删除为a.active错误。

$(".navigation li a").on('click',function(e)
{
    e.preventDefault();
    $(".navigation li a.active").removeClass("active");
    $(this).addClass("active");
    $(activeTab).show();
    return false;  
});  

答案 1 :(得分:0)

active元素中添加了a类,要访问它,您需要使用li而不是$(".navigation li a")

$(".navigation li").removeClass("a.active")

演示:Fiddle

我还建议使用.on

使用事件委派
$(".navigation li").click(function() {  
    $(".navigation li a.active").removeClass("active"); //Remove any "active" class  
    $('a', this).addClass("active"); //Add "active" class to selected tab  


    $(activeTab).show(); //Fade in the active content  
    return false;  
});  

答案 2 :(得分:0)

你可以尝试这个:

$(".navigation li a").click(function(e) { 
    e.preventDefault(); 
    $(this).parent().siblings().find('a').removeClass("active");
    $(this).addClass("active"); //Add "active" class to selected tab  
    $(activeTab).show(); //Fade in the active content  
});  

DEMO FIDDLE

而不是在这个答案中:

$(this).parent().siblings().find('a').removeClass("active");

你也可以试试这个:

$(".navigation a").removeClass("active");

所以:

$(".navigation li a").click(function(e) { 
    e.preventDefault(); 
    $(".navigation a").removeClass("active");
    $(this).addClass("active"); //Add "active" class to selected tab  
    $(activeTab).show(); //Fade in the active content  
});