无法获得多个链接以将单个翻转更改为工作

Cannot get multiple links to change single rollover to work

本文关键字:翻转 单个 工作 链接      更新时间:2023-09-26

我不能得到多个链接工作,我一直得到未捕获的参考错误:不能设置属性"imgToChange"的null(…)谁能告诉我我哪里做错了?

"use strict";
window.onload = rolloverInit;
function rolloverInit() {
    for(var i = 0;i<document.links.length;i++) {
        var linkObj =document.links[i];
        if(linkObj,"id") {
            var linkObj =document.getElementById(linkObj.caption);
            if("imgObj") {
                setupRollover(linkObj,"imgObj");
            }
        }
    }
}
function setupRollover(theLink,theImage) {
    theLink.imgToChange = theImage;
    theLink.onmouseout = function() {
        this.imgToChange.src = this.outImage.src;
    }
    theLink.onmouseover = function () {
        this.imgToChange.src = this.overImage.src;
    }
    theLink.outImage = new Image();
    theLink.outImage = textImage.src;
    theLink.overImage = new Image();
    theLink.overImage.src = "images/" + theLink.id + "Text.gif";
}
 function setupRollover(theLink,australia) {
    theLink.imgToChange = "australiamap";
    theLink.onmouseout = function () {
        this.imgToChange.src = this.outImage.src;
    }
    theLink.onmouseover = function() {
        this.imgToChange.src = this.overImage.src;
    }
    theLink.outImage = new Image();
    theLink.outImage = "images/australiamap.jpg";
    theLink.overImage = new Image();
    theLink.overImage.src = "images/" + caption.id + "adelaide5.jpg";
    }

您似乎将null传递给您的函数,请在调用它之前尝试检查:

function rolloverInit() {
    for(var i = 0;i<document.links.length;i++) {
        var linkObj =document.links[i];
        if(linkObj,"id") {
            var linkObj =document.getElementById(linkObj.caption);
            if(linkObj !== null) { //check if linkObj is not null
                setupRollover(linkObj,"imgObj");
            }
        }
    }
}