2010-03-03 62 views
0

選項卡菜單之間的「當前」類下面的HTML代碼,即:移動基於選擇

<div id="listnav"> 
    <ul id="nav"> 
    <li id="home"><a href="#" id="current">Home</a></li> 
    <li id="features"><a href="#">Features</a></li> 
    <li id="whysp"><a href="#">Why sP</a></li> 
    <li id="screenshots"><a href="#">Screen Shots</a></li> 
    <li id="faq"><a href="#">FAQ</a></li> 
    <li id="contactus"><a href="#">Contact Us</a></li> 
    </ul> 
</div> 

當第一次進入該網頁,我的主頁選項卡設置爲基於href標籤內的id =「current」的電流。

我的問題是,當用戶點擊「功能」選項卡時,我如何通過jQuery,從主頁選項卡中刪除class = current並添加到功能a href標籤,以便我的功能選項卡現在顯示作爲目前的標籤等與其他人?

我也有以下的jQuery功能:

$("#home").click(function(){ 
    $("#content").load('home.html'); 
}); 

$("#features").click(function(){ 
    $("#content").load('features.html'); 
}); 

任何幫助將是巨大的。

謝謝。

+0

你輸入的是「刪除id = current「,我想你的意思是,」刪除class = current「。 – Pointy 2010-03-03 13:33:50

+0

感謝Pointy - 已更新我的帖子。 – tonyf 2010-03-03 13:52:06

回答

4
$('#nav a').click(function(){ 
    $("#nav").find("a").removeAttr("id"); 
    $(this).attr("id" , "current"); 
}); 
+0

感謝大家,感謝拉胡爾 - 完美的工作。 – tonyf 2010-03-03 13:52:47

0

試試這個:

$('#nav a').click(function(){ 
$(this).attr('id','current').parent('li').siblings() 
     .children('a').removeAttr('id'); 
}); 
0

我想你可以使用一個名爲「當前」類,然後刪除/添加使用.removeClass().addClass()

0
$("#features").click(function(){ 
    $("#current").removeAttr("id"); 
    $("#features a").attr("id", "current"); 
    $("#content").load('features.html'); 
}); 

我會建議使用類,而不是當前的ID

0

沒有必要重複您的導航代碼爲每個鏈接:-)

$("#nav li a").click(function() 
{ 
    // Remove old current 
    $("#current").removeAttr("id"); 
    // Set new current 
    $(this).attr("id", "current"); 
    // Load content for clicked nav 
    $("#content").load($(this).closest("li").attr("id") + '.html'); 
    // Prevent click from jumping to the top of the page 
    return false; 
});