关于javascript的鼠标事件(初学者)

2017-09-08 00:00 出处:360java.com 作者:360java  阅读()
function setupRollover(thisImage) { thisImage.outImage = new Image(); thisImage.outImage.src = thisI
function setupRollover(thisImage) {
        thisImage.outImage = new Image();
        thisImage.outImage.src = thisImage.src;
        thisImage.onmouseout = function() {
                this.src =thisImage.outImage .src;
        }

        thisImage.clickImage = new Image();
        thisImage.clickImage.src = "images/" + thisImage.id + "_click.gif";
        thisImage.onclick = function() {
                this.src = thisImage.clickImage.src;
        }
 
        thisImage.overImage = new Image();
        thisImage.overImage.src = "images/" + thisImage.id + "_on.gif";
        thisImage.onmouseover = function() {
                this.src = thisImage.overImage.src;
        }
}

 

最近在学习javascript,在书上看到了这样的一串代码,意思是鼠标在图片上,点击图片,不在图片上的效果,我不明的是为什么要定义outImage,clickImage,overImage,所以我改了一下代码

function setupRollover(thisImage) {
        temp = new Image();
        temp.src = thisImage.src;
        thisImage.onmouseout = function() {
                this.src = temp.src;
        }

        
        temp.src = "images/" + thisImage.id + "_click.gif";
        thisImage.onclick = function() {
                this.src = temp.src;
        }

        temp.src = "images/" + thisImage.id + "_on.gif";
        thisImage.onmouseover = function() {
                this.src = temp.src;
        }
}

发现不行,我不明白是为什么,请高人指点!

< >
最佳答案

这是本人的理解:在页面加载完成后这段代码初始化了一次,temp.src = "images/" + thisImage.id + "_on.gif";初始化完成之后,temp.src的值就是这个。所以,不管把鼠标放到图片上、离开图片、点击图片this.src的值都等于temp.src,也就是"images/" + thisImage.id + "_on.gif"。这是我改过的代码:

function setupRollover(thisImage) {
    temp = new Image();
    thisImage.onmouseout = function() {
        temp.src = "images/" + thisImage.id + "_off.gif";
        this.src = temp.src;
    }

    thisImage.onclick = function() {
        temp.src = "images/" + thisImage.id + "_click.gif";
        this.src = temp.src;
    }

    thisImage.onmouseover = function() {
        temp.src = "images/" + thisImage.id + "_on.gif";
        this.src = temp.src;
    }
}
分享到:
本文标签:

相关文章

发表评论(共条评论)愿您的每句评论,都能给大家的生活添色彩,带来共鸣,带来思索,带来快乐。

Copyright (C) java学习 360java 360java.com, All Rights Reserved.

苏ICP备16022210号