日期:2014-05-16  浏览次数:20358 次

DOM中this的疑问
function changeBackgroundColor() {
            var changeColor = document.getElementById("changeColor");
            var changes = changeColor.getElementsByTagName("a");
            for (var i = 0; i < changes.length; i++) {
                var change = changes[i];
                change.onclick = function () {
                    var changeColor = document.getElementById("changeColor");
                    var changes = changeColor.getElementsByTagName("a");
                    for (var i = 0; i < changes.length; i++) {
                        var change = changes[i];
                        if (this == change) {
                            change.style.background = "red";
                        }
                        else {
                            change.style.background = "white";
                        }
                    }
                    return false;
                }
                
            }
        }

如果把匿名函数function()里if判断后的changes改为this(style之间那个),则点第一个连接有效果,第二个就没有,请问这是什么原理?
------解决方案--------------------
function changeBackgroundColor() { 
    var changeColor = document.getElementById("changeColor"); 
    var changes = changeColor.getElementsByTagName("a"); 
    for (var i = 0; i < changes.length;&nb