2013-08-28 49 views
0

我想改變點擊的UL LI鏈接與jquery;它只適用於懸停事件,不適用於點擊事件。請參閱下面我的代碼:試圖chage UL LI背景顏色jquery

$("nav ul li").hover(function() { 
    $(this).find('a').css('color', '#FF0'); 
}, function() { 
    $(this).find('a').css('color', '#FFF'); 
}); 

$("nav ul li").click(function() { 
    $(this).find('a').css('color', '#FF0'); 
    $(this).find('a').siblings().css('color', '#FFF'); 
}); 
+0

對不起,我的意思是改變鏈接的顏色... – user2725341

+1

$(this).find('a')。siblings()'可能會匹配比你想要的更多的元素。你能詳細說明你想達到什麼,所以我們可以提出更好的解決方案嗎? (發佈你的標記的例子也有幫助。) –

+0

你可以發佈相關的標記嗎? – billyonecan

回答

0
$(function() {      //run when the DOM is ready 
$(".clickable").click(function() { //use a class, since your ID gets mangled 
    $(this).addClass("active");  //add the class to the clicked element 
}); 
}); 

使用這種方法,你可以簡單地用一組新上點擊屬性替換類。

0

也許,如果點擊你的錨,那麼你navigationg到其他頁面,如果你因爲某些原因至極,以避免它和做的東西你的方式,防止錨點擊默認的導航操作(導航)

$("nav ul li a").click(function(e) { 
    e.preventDefault(); 
});