2016-01-05 19 views
0

由於我在學習jQuery的一開始,我希望有人可以幫我解決這個問題:我有一個隱藏/顯示內容時的代碼導航項目被選中。所選的導航項目也被設置爲粗體。升級簡單的顯示/隱藏jQuery:顯示第一個內容+大膽的導航項目

現在我想將它升級,以便:。選擇

B中所述的導航項目之前

一)第一內容(menu_apps)是可見的)將相應的第一個nav-item(show_apps)設置爲粗體,以便人們可以看到它已鏈接到可見內容。

我一直試圖讓它工作,但每次都失敗。真的好奇看到解決方案。謝謝你的時間!

代碼:http://jsfiddle.net/KUtY5/489/

JS

$(document).ready(function(){ 
    $("#nav a").click(function(){ 
     $("#nav a").css("font-weight", 400); // First you make them thin 
     $(this).css("font-weight", 800); // Than you make them bold 
      var id = $(this).attr('id'); 
     id = id.split('_'); 
     $("#menu_container div").hide(); 
     $("#menu_container #menu_"+id[1]).show(); 
    }); 
}); 

CSS

#menu_container { 
width: 650px; 
height: auto; 
padding-left: 30px; 
} 
#menu_container div { 
display:none; 
} 

HTML

<div id='nav'> 
    <a id="show_apps">Appetizers</a> | <a id="show_soups">Soups and Salads</a> | <a id="show_entrees">Entrees</a> 
</div> 

<div id="menu_container"> 
    <div id="menu_apps"> 
    Content of the App Section Here 
    </div> 
    <div id="menu_soups"> 
    Content of the Soups Section Here 
    </div> 
    <div id="menu_entrees"> 
    Content of the Entrees Section Here 
    </div> 
</div> 
+0

我沒有太多的補充到其他的答案,但我會建議一般來說,你定義了一些CSS類,並添加/刪除這些,而不是用'css()'編程改變個別樣式屬性。通過用類標記元素,不僅可以改變它們的外觀,還可以通過jQuery選擇器在腳本的其他地方以編程方式選擇和操作它們。例如,在一個包含多個選定行的表格中,您可以使用'$('tr.highlighted')'獲得這些行的列表,其中'highlight'是使它們看起來被選中的CSS類的名稱。 – zerobandwidth

+0

感謝您的有益建議@zerobandwith :) – TimVanGorp

回答

0

在這裏,我已經更新了你的代碼有點檢查出來 http://jsfiddle.net/KUtY5/493/ 我已經添加了這些線路上的文檔準備

$("#menu_apps").show(); 
$("#show_apps").css("font-weight", 400); // First you make them thin 
$('#show_apps').css("font-weight", 800); 
+0

非常感謝你艾哈邁德!祝你有個美好的一天:) – TimVanGorp

0

試試這個:

$(document).ready(function(){ 
$("#nav a").click(function(){ 
$("#nav a").css("font-weight", 400); // First you make them thin 
$(this).css("font-weight", 800); // Than you make them bold 
    var id = $(this).attr('id'); 
    id = id.split('_'); 
    $("#menu_container div").hide(); 
    $("#menu_container #menu_"+id[1]).show(); 
}); 
    $("#nav a:first").css("font-weight", 800); 
}); 
0

它會爲你工作..謝謝。

$(document).ready(function(){ 
 
$("#menu_container div").hide(); 
 
$("#menu_container #menu_"+'apps').show(); 
 
$("#show_apps").css("font-weight", 800); 
 

 
    $("#nav a").click(function(){ 
 
    \t \t $("#nav a").css("font-weight", 400); // First you make them thin 
 
    \t $(this).css("font-weight", 800); // Than you make them bold 
 
      var id = $(this).attr('id'); 
 
     id = id.split('_'); 
 
     $("#menu_container div").hide(); 
 
     $("#menu_container #menu_"+id[1]).show(); 
 
    }); 
 
});
#menu_container{ 
 
    width: 650px; 
 
    height: auto; 
 
    padding-left: 30px; 
 
} 
 

 
#menu_container div{display:none;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id='nav'> 
 
     <a id="show_apps">Appetizers</a> | <a id="show_soups">Soups and Salads</a> | <a id="show_entrees">Entrees</a> 
 
    </div> 
 
    
 
    
 
<div id="menu_container"> 
 
    <div id="menu_apps"> 
 
    Content of the App Section Here 
 
    </div> 
 
    <div id="menu_soups"> 
 
    Content of the Soups Section Here 
 
    </div> 
 
    <div id="menu_entrees"> 
 
    Content of the Entrees Section Here 
 
    </div> 
 
</div>

+0

非常感謝你的時間Renuka!有一個偉大的一天:) – TimVanGorp