2012-10-23 56 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

相关问题