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>

此代码段是在单击事件时更改两个按钮的颜色,在 chrome 和 firefox 中工作正常,但 onclick 功能在 IE9 中不起作用。请帮助...在此先感谢

4

1 回答 1

1

尝试调用函数

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

也可以;在每个函数表达式之后使用,即myButton[0].onclick = function() {...};.

在这里工作

于 2012-10-23T17:10:03.983 回答