0

我正在尝试将菜单与活动选项一起放置。当我使用 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>
4

3 回答 3

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;  
});  
于 2013-03-19T07:49:16.557 回答
0

该类active添加了a元素内部的li元素,要访问它,您需要使用$(".navigation li a")not $(".navigation li").removeClass("a.active")

$(".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;  
});  

演示:小提琴

我还建议使用.on使用事件委托

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


    //Commented since it is not available
    //$(activeTab).show(); //Fade in the active content  
    return false;  
});  
于 2013-03-19T07:50:59.177 回答
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  
});  

演示小提琴

而不是这个答案:

$(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  
});
于 2013-03-19T07:58:46.657 回答