2012-10-23 92 views
0
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 



<script language="javascript" type="text/javascript"> 
function abc() 
{ 
ansArray = ['a']; 
document.write('<input type = "button" value = "a">'); 
document.write('<input type = "button" value = "b">'); 
var myButton = document.getElementsByTagName("input"); 

myButton[0].onclick = function() { 
    if(ansArray[0] == 'a') 
     myButton[0].style.backgroundColor = "green"; 
    else 
     myButton[0].style.backgroundColor = "red"; 
} 

myButton[1].onclick = function() { 
    if(ansArray[0] == 'b') 
     myButton[1].style.backgroundColor = "green"; 
    else 
     myButton[1].style.backgroundColor = "red"; 
} 
} 
</script> 
</head> 

<body onload="abc()"> 
</body> 
</html> 

該代碼段是改變click事件的兩個按鈕的顏色發生故障,工作正常,在Chrome和Firefox,但的onclick功能不IE9工作。請幫助...在此先感謝代碼在Chrome和Firefox,但在IE9

+0

我認爲IE需要一個分號後onclick函數 –

+0

只需通過一個調試器,看看有什麼不工作。 – asawyer

回答

1

嘗試調用該函數像

(function abc(){ 
    // code here 
})(); 

還可以使用;每個函數表達式後,即myButton[0].onclick = function() {...};

Working here

+0

調試器不顯示任何錯誤(控制檯),我在每個函數之後也使用了分號。在實際情況下,函數足夠大,可寫爲(function abc(){ // code here })(); – user1767304

+0

(function abc(){ // code here })(); - 工作正常謝謝 – user1767304

相關問題