JQuery选择了tab问题?

时间:2010-03-05 06:49:13

标签: jquery html css

我是JQuery的新手,由于某种原因,我无法正确选择当前选中的标签,选择后它应该是不同的颜色。有人可以帮我解决这个问题吗?

这是JQuery。

$(document).ready(function() {

    //When page loads...
    $(".form-content").hide(); //Hide all content
    $("#menu ul li:first").addClass("selected-link").show(); //Activate first tab
    $(".form-content:first").show(); //Show first tab content

    //On Click Event
    $("#menu ul li").click(function() {

        $("#menu ul li").removeClass("selected-link"); //Remove any "selected-link" class
        $(this).addClass("selected-link"); //Add "selected-link" class to selected tab
        $(".form-content").hide(); //Hide all tab content

        var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the selected-link tab + content
        $(activeTab).fadeIn(); //Fade in the selected-link ID content
        return false;
    });

});

这是CSS。

#container {
    width: 1024px;
    background: blue;
    padding: 0px;
    margin: 0px;
    float: left;
}

#menu {
    padding: 0px;
    margin: 0px;
}


#menu ul {
    width: 1024px;
    padding: 0px;
    margin: 0px;
    margin-top: 25px;
    border: 0px; 
    float: left;
    text-align: left;
    list-style-type: none;
}

#menu li {
    margin: 0px;
    margin-right: 1px;
    padding: 0px;
    float: left;
    border: 0px;
    width: auto;
}

#menu a:link, #menu a:visited {
    padding: 9px 9px;
    float: left;
    color: white;
    text-decoration: none;
    background: black;
    width: auto;
}

#menu a.selected-link, #menu a:hover {
    background: blue;
    color: white;
}

这是HTML。

    <div id="body-content">

        <div id="menu">
            <ul>
                <li><a href="#tab-1" title="">tab 1</a></li>
                <li><a href="#tab-2" title="">tab 2</a></li>
                <li><a href="#tab-3" title="">tab 3</a></li>
                <li><a href="#tab-4" title="">tab 4</a></li>
                <li><a href="#tab-5" title="">tab 5</a></li>
            </ul>
        </div>


        <div id="container">

            <div id="tab-1" class="form-content">
                <p>tab 1</p>
            </div>

            <div id="tab-2" class="form-content">
                <p>tab 2</p>
            </div>

            <div id="tab-3" class="form-content">
                <p>tab 3</p>
            </div>

            <div id="tab-4" class="form-content">
                <p>tab 4</p>
            </div>

            <div id="tab-5" class="form-content">
                <p>tab 5</p>
            </div>      

        </div>

    <div>

2 个答案:

答案 0 :(得分:1)

尝试更改这些行:

$("#menu ul li").removeClass("selected-link");
$(this).addClass("selected-link");

要:

$("#menu ul li").find('a').removeClass("selected-link");
$(this).find('a').addClass("selected-link");

您想将课程添加到link而不是li

答案 1 :(得分:0)

<强> 修改

$(document).ready(function() {

    //When page loads...
    $(".form-content").hide(); //Hide all content
    var firstMenu = $("#menu ul li:first");
    firstMenu.show();
    firstMenu.find("a").addClass("selected-link"); //Activate first tab
    $(".form-content:first").show(); //Show first tab content

    //On Click Event
    $("#menu ul li").click(function() {

        $("#menu ul li a").removeClass("selected-link"); //Remove any "selected-link" class
        $(this).find("a").addClass("selected-link"); //Add "selected-link" class to selected tab
        $(".form-content").hide(); //Hide all tab content

        var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the selected-link tab + content
        $(activeTab).fadeIn(); //Fade in the selected-link ID content
        return false;
    });

});