2010-01-14 54 views
1

我想選擇一個表格行,並用Javascript將其突出顯示。然而,似乎沒有任何工作。當我註釋掉以下代碼的前兩行時,我可以突出顯示,但是當我單擊另一行時,先前選擇的行保持高亮顯示而不是回到白色。Javascript Table Select

var selectedEventId = 0; 

    function SelectRow(tableRow){ 
     var SelectedRow = Document.getElementById('selectedEventId'); 
     SelectedRow.style.backgroundColor = 'white'; 

     var frame = document.getElementById('additionalText'); 
     frame.src="iframeContents.php?id="+tableRow.id; 
     selectedEventId = tableRow.id; 
     tableRow.style.backgroundColor = '3366ff'; 
     var prevRow = document.getElementById('selectedEventId'); 
     return; 

    }//end SelectRow 

任何幫助,將不勝感激。

回答

1

嘗試了這一點:


var selectedEventId = 0; 
var prevRow = ''; 
function SelectRow(tableRow){ 
    if (prevRow != '') { prevRow.backgroundColor = 'white'; } 
    var frame = document.getElementById('additionalText'); 
    frame.src="iframeContents.php?id="+tableRow.id; 
    selectedEventId = tableRow.id; 
    tableRow.style.backgroundColor = '3366ff'; 
    // based on the assumption that selectedEventId is set as a global variable 
    prevRow = document.getElementById(selectedEventId); 
    return; 
} 
+0

YAY!這有效,除了在if語句中,我不得不做prevRow.style.backgroundColor ='white';非常感謝!!!!! – 2010-01-14 13:14:07

0

你應該調用document.getElementById(selectedEventId)嗎? (沒有引號)

1

替換

var SelectedRow = Document.getElementById('selectedEventId'); 

var SelectedRow = document.getElementById(selectedEventId); 

由於selectedEventId是一個變量。在第一個代碼片段中,它將selectedEventId作爲字符串處理。

你的IDS不應以數字開頭。

相關問題