2013-12-09 66 views
1

即時通訊使用Smarty和php4(無法升級=()。在我的網站左側,我有一個導航欄與我所有的鏈接。紅色和保持紅色,直到我點擊另一個鏈接。我已經發現了一些當谷歌搜索,但我不能讓它工作艱難,大家都說它運行完美:/ .....腳本改變顏色爲紅色,當我點擊一個鏈接但未能保持紅顏色後,我的網址更改。更改頁面時保持鏈接樣式Javascript

{literal} 
<script type="text/javascript"> 
// first component: non-active link-color 
// second component: active link-color 
var arrColor = new Array("#404040","#e40624"); 

var lnkOldID = ""; 
var lnkOldColor; 

// get object-type 
IE = document.all&&!window.opera; 
DOM = document.getElementById&&!IE; 


function activeLink(lnkID){ 
    // declare object 
    myObj = (IE)?document.all[lnkID]:document.getElementById(lnkID); 
    // if no link is chosen 
    if(lnkOldID == ""){ 
     lnkOldID = lnkID; // remember cur. ID 
     lnkOldColor = arrColor[0]; // remember cur. color 
     myObj.style.color = arrColor[1]; // change color 
    }else{ 
     // if current link is not active link 
     if(lnkOldID != lnkID){ 
      // declare object 
      var myOldObj = (IE)?document.all[lnkOldID]:document.getElementById(lnkOldID); 
      myOldObj.style.color = lnkOldColor; // set color to non-activelink color 
      myObj.style.color = arrColor[1];  // setcolor to activelinkcolor 
      lnkOldID = lnkID;  // remember cur. ID 
      lnkOldColor = arrColor[0]; // remember cur. color 
     } 
    } 
} 
</script> 
{/literal} 

<a onClick="activeLink('verteilung_prios')" id="verteilung_prios" href="?n=navigation_pm&a=verteilung_prios">Verteilung Prios</a> 
<a onClick="activeLink('histogramm_reaktionszeiten')" id="histogramm_reaktionszeiten" href="?n=navigation_pm&a=histogramm_reaktionszeiten">Histogramm Reaktionszeiten</a> 

回答

1

如何改變類?

sample

腳本:

function toggleColor(obj) { 
    var redObjs = document.getElementsByClassName('red'); 

    for (var i = 0; i < redObjs.length; i++) { 
     redObjs[i].className = ''; 
    } 

    obj.className = 'red'; 
} 

添加類功能時,頁面刷新 在document.onload

function initClass() { 
    // get now url 
    //var nowUrl = document.location.href; 
    // dummy url 
    var nowUrl = 'http://fiddle.jshell.net/now/'; 

    // get elements that have 'navi' class 
    var naviObjs = document.getElementsByClassName('navi'); 

    for (var i = 0; i < naviObjs.length; i++) { 
     if (naviObjs[i].href == nowUrl) { 
      // check url 
      //alert(naviObjs[i].className); 

      // add 'red' class 
      naviObjs[i].className += ' red'; 
     } 
    } 
} 

CSS執行:

a:visited 
{ 
    color: #404040; 
} 

a.red 
{ 
    color: #e40624; 
} 
+0

THX的幫助,但同樣的問題之前。當URL更改風格重置 – user3091456

+0

你可以設置類到現在由Smarty URL標記?像這樣

相關問題