2

我正在努力完成这项工作......我已经工作了好几个小时,但无法弄清楚。我想让它在如果单击 OF 时,悬停状态保持活动状态,如果单击另一个链接,例如,Founders,在 OF 上处于活动状态的悬停状态消失,悬停状态在 Founders 上保持活动状态。我哪里错了?

HTML:

<div id="profile_list">
     <h2>Members: 37</h2>
 <a href="#Original_Founder" class="panel">• O.F.</a>
 <a href="#Founder" class="panel">• Founder</a>
 <a href="#Leader" class="panel">• Leader</a>
 <a href="#Senior_Admin" class="panel">• Sr. Admin</a>
 <a href="#Junior_Admin" class="panel">• Jr. Admin</a>
 <a href="#Full_Member" class="panel">• Full-Member</a>
 <a href="#Greenhorn" class="panel">• Greenhorn</a>
 <a href="#Inactive" class="panel">• Inactive</a>
 <a href="#Legend" class="panel">• Legend</a>

</div>

CSS:

#profile_list {
    width: 250px;
    height: 328px;
    background-color: #333;
    background-image: -moz-linear-gradient(#777, #222);
    background-image: -webkit-gradient(linear, left top, left bottom, from(#777), to(#222));
    background-image: -webkit-linear-gradient(#777, #222);
    background-image: -o-linear-gradient(#777, #222);
    background-image: -ms-linear-gradient(#777, #222);
    background-image: linear-gradient(#777, #222);
    border: 1px solid #000;
    -moz-border-radius: 10px;
    -webkit-border-radius: 10px;
    border-radius: 10px;
    -moz-box-shadow: 0 28px 24px -24px #000, inset 0 -0.3em 0.9em 0.3em #000;
    -webkit-box-shadow: 0 28px 24px -24px #000, inset 0 -0.3em 0.9em 0.3em #000;
    box-shadow: 0 28px 24px -24px #000, inset 0 -0.3em 0.9em 0.3em #000;
    float: left;
    position: relative;
    top: 20px;
    left: 20px;
    z-index: 2;
}
#profile_list h2 {
    width: 226px;
    height: 20px;
    padding: 10px 0;
    margin: 0 12px;
    border-bottom: 1px solid #444;
    float: left;
    color: #B45F04;
    font: 20px Arial, Helvetica, sans-serif;
    font-weight: bold;
    font-variant: small-caps;
    text-shadow: 1px 1px 1px #000, -2px -2px 2px #000;
}
#profile_list a {
    width: 218px;
    height: 20px;
    padding: 4px 12px 7px 20px;
    color: #A4A4A4;
    float: left;
    font: 18px Arial, Helvetica, sans-serif;
    font-weight: bold;
    font-variant: small-caps;
    text-decoration: none;
    text-shadow: 1px 1px 1px #000, -2px -2px 2px #000;
    position: relative;
    top: 5px;
}
#profile_list a:hover, #profile_list a.active {
    background: rgba(204, 204, 204, 0.5);
    -moz-box-shadow: inset 0 -0.3em 0.9em 0.3em #000;
    -webkit-box-shadow: inset 0 -0.3em 0.9em 0.3em #000;
    box-shadow: inset 0 -0.3em 0.9em 0.3em #000;
    color: #FFF;
}

JAVASCRIPT:

$(document).ready(function () {
    $('a.profile_list').click(function () {
        var a = $(this);
        $(this).addClass('.active');
    });
});

这是我到目前为止的演示,但是当我单击任何链接时,悬停状态不会保持活动状态:http: //jsfiddle.net/WgdXU/2/

4

4 回答 4

2

代替

$(this).addClass('.active');

$(this).addClass('active');

您不需要添加 . 对于添加类

替换

$('a.profile_list').click(function () {

$('#profile_list a').click(function () {

看演示

评论 编辑

http://jsfiddle.net/WgdXU/8/

于 2013-09-20T08:12:22.787 回答
1

试试这个:- http://jsfiddle.net/aidioo7/WgdXU/7/

JS:-

$(document).ready(function () {
    $('#profile_list a').click(function () {
        $('#profile_list a').removeClass("active");
        $(this).addClass('active');
    });
});
于 2013-09-20T08:14:11.840 回答
0

更改您的脚本如下:

$(document).ready(function () {
$('a.panel').click(function () {
    var a = $(this); // no need of having this variable as it is not being used. You can remove it.
    $(".active").removeClass("active");
    $(this).addClass('active');
});
});
于 2013-09-20T08:10:44.363 回答
0

由于“profile_list”是一个 id,所以使用“#profile_list”而不是“.profile_list”。此外,对于添加活动类,您无需使用“。”

请试试这个。

$(document).ready(function () {
$('#profile_list a').click(function () {
    var a = $(this);
    $('#profile_list a').removeClass('active');
    $(this).addClass('active');
});
}); 

谢谢

于 2013-09-20T08:21:13.833 回答