2013-12-13 40 views
0

我有一個母版頁中的菜單項..我用javascript來改變類,當點擊..它改變了一秒類,但當其重定向到其他頁面它成爲同..任何想法如何,我應該做chaning在母版頁中的菜單項的類

我沒有獲得完整的代碼儀式,但現在它是這樣的(我已經使用了一些警告框,只是爲了看看發生了什麼..)

<style> 
test1{ color: red; 
    height:1000px; 
     font-size:5px ; 
     background-color:Aqua; 
     } 

     test2 
     { font-family:Kozuka Mincho Pro M; 
      color: red; 
     font-size: 5px } 

<script type="text/javascript"> 
var lastid = ""; 
function myFunc(id) { 
    alert(id); 
    if (lastid != "") { 
     document.getElementById(lastid).removeAttribute("class", "test2"); 
     document.getElementById(lastid).setAttribute("class", "test1"); 
     var a = document.getElementById(lastid); 

     alert(a.getAttribute("class")); 

    } 
    var element = document.getElementById(id); 
    document.getElementById(id).setAttribute("class", "test2"); 
    var cssid = id; 
    $("#" + id).addClass('test1'); 


    alert(document.getElementById(id).getAttribute("class")); 
    lastid = id; 

} 
    </script> 


<li id="firstry" onclick="myFunc(this.id);" ><a href="master-child.aspx"> Click</a> </li> 
+1

如果你想在類值跨頁堅持,你需要發在某處(如cookie,會話或數據庫)撕裂類的價值(因爲它與用戶有關)。既然你是用javascript來做這件事,那麼cookie會是最容易的。 – MikeSmithDev

回答

1

我已經做到了這一點使用jQuery ..它容易得多做的事情..這正好通過菜單的每一個環節,然後將下面的它與當前的頁面相匹配的類.. 是腳本:

<script type="text/javascript"> 
     $(document).ready(function() { 
      $('#subnav a').each(function (index) { 
       if (this.href.trim() == window.location) 
        $(this).addClass("selected"); 
      }); 
     }); 
    </script> 
0

做到這一點的一種方法是將一個類添加到對應於頁面名稱的body標籤和一個CSS類。

<body id="home"> 

,那麼Css活動菜單項,假設你在你的菜單項具有類名稱:

#home li.firstitem { ... } 
#about li.seconditem { ... }