简体   繁体   English

onclick javascript函数在IE中不起作用

[英]onclick javascript function does not work in IE

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";
}

onclick function not working in my above example(IE9), but they work fine in Chrome and Firefox. onclick函数在上面的示例(IE9)中不起作用,但是在Chrome和Firefox中它们可以正常工作。

The issue is that your DOM is not yet loaded when you are trying to access the buttons. 问题是,当您尝试访问按钮时,尚未加载DOM。 Wrap your onclick handlers in window.load and everything should work fine: 将onclick处理程序包装在window.load中,一切都应该正常工作:

window.onload = function () {
    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";
        }
    }
}

I am actually surprised this works under Webkit and Mozilla. 实际上,我很惊讶Webkit和Mozilla下的这种功能。 I have created a small demo fiddle . 我创建了一个小演示小提琴 In all cases, in all browsers the object comes out as null before load, unless the script block is after the element you are accessing inside the body. 在所有情况下,在所有浏览器中,对象在加载之前均显示为null,除非脚本块位于您要在主体内部访问的元素之后。

Notice though that there is a difference in how getElementsByTagName reacts inside different browsers, it is different than getElementById : fiddle 请注意,虽然有如何的不同getElementsByTagName内不同的浏览器反应,它比是不同getElementById小提琴

Another alternative would be to not wait for window.onload would be to wait for document.body because window.onload happens after all content including images is loaded. 另一种选择是不等待window.onload ,而是等待document.body因为window.onload在包含图像的所有内容加载之后发生。

function Start() {
    if (document.body) {
        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";
            }
        }
    }
}

setInterval(Start, 50); // 50 ms is a small enough interval to retry

我在这里使用的相同答案Javascript onclick函数不起作用应该适用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM