jQuery--改變導航背景色

 css樣式:css

  
  
           
  
  
  1. .tgmenu{width:950pxheight:33pxbackground:#9D291Cborder-top:2px solid #DC442Fmargin:0 autoline-height:34pxpadding-left:30px
  2. .tgmenu a{ font-size:14pxtext-decoration:nonefont-weight:bold;loat:leftcolor:#FFF
  3. .tgmenu .Mcurrent{background:url(../p_w_picpaths/tg_sprite.gif) repeat-x;color:#CC3333;border-left:2px solid #DC442F;height:33px;float:lefttext-align:centerpadding:0 20px
  4. .tgmenu .Mlink{border-left:2px solid #DC442F;height:33px;float:lefttext-align:centerpadding:0 20px

html代碼:html

  
  
           
  
  
  1. <div class="tgmenu" id="menu"> 
  2.             <a><span id="span1" class="Mcurrent" onclick="MenuClick(this.id,'GroupBuyNow.aspx')">今日團購</span></a> 
  3.             <a><span id="span2" class="Mlink" onclick="MenuClick(this.id,'GroupBuyPast.aspx')">往期團購</span></a> 
  4. </div> 

jQuery:ide

  
  
           
  
  
  1. function MenuClick(id,str) {  
  2.             $("#menu span").each(function () { 
  3.                 $(this).attr("class""Mlink"); 
  4.             }); 
  5.             $("#" + id).attr("class""Mcurrent"); 
  6.         }      

另外一種方法:this

來自:http://topic.csdn.net/u/20101124/16/f6fd651e-0dde-4dc0-b52d-45d04b0428b7.htmlurl

  
  
           
  
  
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  2. <html> 
  3. <head> 
  4. <title> New Document  </title> 
  5. <meta name="Generator" content="EditPlus">  
  6. <meta name="Author" content="">  
  7. <meta name="Keywords" content="">   
  8. <meta name="Description" content="">   
  9. <style>          
  10. ul,li{padding:0;margin:0;list-style:none}    
  11. #menu li{width:100px;background:#EEE;margin-right:1px;display:block;float:left;height:24px;line-height:24px;vertical-align:middle;text-align:center}     
  12. #menu li:first-child{background:#CCC;}       
  13. </style>    
  14. <script>   
  15. function MenuClick(obj){   
  16. var tag = document.getElementsByTagName("li")  
  17. for (var i=0;i <tag.length;i++ )     
  18. {            
  19. tag[i].style.background="#EEE" ;     
  20. obj.style.background = "#ccc"   ; }      
  21. }    
  22. </script>    
  23. </head>    
  24. <body>     
  25. <ul id="menu">   
  26. <li onclick="MenuClick(this)">首頁 </li>       
  27. <li onclick="MenuClick(this)">頁面1 </li>   
  28. <li onclick="MenuClick(this)">頁面2 </li>  
  29. <li onclick="MenuClick(this)">頁面3 </li>    
  30. <ul>   
  31. </body>   
  32. </html>
相關文章
相關標籤/搜索