注册 登录
编程论坛 JavaScript论坛

var imgObj = document.getElementById(linkObj.className);中classname 取的值是什么,大家帮帮我看

sunbenlong 发布于 2009-08-21 13:46, 1510 次点击
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html xmlns="http://www.
<head>
    <title>Multiple Links, Single Rollover</title>
    <script src="script05.js" language="javascript" type="text/javascript"></script>
</head>
<body bgcolor="#EECC99">
    <img src="images/DaVinci.jpg" width="144" height="219" alt="DaVinci" align="right" hspace="50" />
    <img src="images/leoText.gif" width="375" height="26" alt="Leonardo's Inventions" />
    <a href="flyPage.html" class="textField" id="flyer"><img src="images/flyer.gif" width="293" height="165" border="0" alt="Flying Machine" vspace="10" id="flyerImg" /></a><br clear="right" />
    <img src="images/bg.gif" width="208" height="27" id="textField" alt="Text Field" align="right" vspace="20" />
    <a href="tankPage.html" class="textField" id="tank"><img src="images/tank.gif" width="325" height="92" border="0" alt="Tank" id="tankImg" /></a><br />
    <a href="heliPage.html" class="textField" id="helicopter"><img src="images/helicopter.gif" width="224" height="160" border="0" alt="Helicopter" id="helicopterImg" /></a>

<p>多个链接触发一个翻转器</p>
</body>
</html>

下面为script05.js
window.onload = rolloverInit;

function rolloverInit() {
    for (var i=0; i<document.links.length; i++) {
        var linkObj = document.links[i];
        
        if (linkObj.className) {                    //因为id必须是唯一的,,我们要使用class属性寻找链接对像classname
            var imgObj = document.getElementById(linkObj.className);
            
            if (imgObj) {
                setupRollover(linkObj,imgObj);
            }
        }
    }
}

function setupRollover(thisLink,textImage) {
    thisLink.imgToChange = textImage;
    thisLink.onmouseout = rollOut;
    thisLink.onmouseover = rollOver;    
    
    thisLink.outImage = new Image();
    thisLink.outImage.src = textImage.src;

    thisLink.overImage = new Image();
    thisLink.overImage.src = "images/" + thisLink.id + "Text.gif";
    
}

function rollOver() {
    this.imgToChange.src = this.overImage.src;
}

function rollOut() {
    this.imgToChange.src = this.outImage.src;
}
0 回复
1