2012-07-20 134 views
0

我在嘗試向我的鏈接添加活動狀態時遇到了一些問題。這個我擺弄代碼:單擊時將活動狀態添加到超鏈接

$(function() { 

    $('a').click(function() { 
     $(this).addClass('PnavCurrent'); 
     //$("div a").removeClass("PnavCurrent"); 
     //$("div a[href="+newHash+"]").addClass("PnavCurrent"); 

     $(this).removeClass('PnavCurrent'); 
     $(this).css('color', '#51ad9d'); 
     $(this).css('z-index', '2'); 
     $(this).css('border-color', '#00a8ff'); 

     $('a').hover($(this).css('z-index', '10')); 
    }); 
}); 

出於某種原因,它不會對鏈接應用CSS活動狀態類(「PnavCurrent」),但是當我在腳本代碼指定它它不正常工作{$(this).css...}。現在我想的是,在添加一個懸停狀態的z-index在腳本代碼的鏈接,這樣的事情:

$('a').hover($(this).css ('z-index', '10')); 

我試圖使用的代碼有點堵在上面努力實現這一目標但它不起作用。我將不勝感激,如果任何人都可以提供幫助,並可能是一個更好的解決方案。

+0

您正在添加,然後刪除班級'PnavCurrent' – 2012-07-20 01:05:37

回答

1

CSS:

.PnavCurrent { 
    color: #51ad9d; 
    z-index: 2; 
    border-color: #00a8ff; 
} 

.PnavCurrent:hover { z-index: 10; } 

的Javascript:

$(function() { 
    $('a').click(function(e) { 
     // you usually want to prevent default for handling link clicks, 
     // otherwise the browser follows the href (if that's intended skip this) 
     e.preventDefault(); 

     $('a').not(this).removeClass('PnavCurrent'); 
     $(this).addClass('PnavCurrent'); 
    }); 
}); 

註上jQuery .hover(): 你不需要這個在這裏的所有,但用法會是這樣(這是一個替代至css :hover規則,不一起使用)

$("a").hover(
    function() { 
     $(this).css('z-index',2)); 
    }, 
    function() { 
     $(this).css('z-index', 10); 
    } 
); 
+0

對不起,很長的響應時間。我認爲我的CSS中的某些內容與此腳本衝突,因爲當我從頭開始測試新文件(html和css)時,它可以工作,但有一些問題,例如它不記得我的鏈接在頁面重新加載時的活動狀態等。有用。非常感謝nbrooks。你一直是一個非常大的幫助,我很欣賞它。 – spencer 2012-07-20 20:26:38