Javascript更改图像的来源

时间:2010-06-07 20:24:15

标签: javascript jquery

我有一些javascript运行一个计时器,可以在网站上激活一些东西。

这一切都很完美,但我希望在动画运行时能够改变图像,它使用jquery:

if(options.auto && dir=="next" && !clicked)
{
    timeout = setTimeout(function(){
if (document.images['bullet1'].src == "img/bulletwhite.png")
{
        document.images['bullet1'].src = "img/bullet.png";
        document.images['bullet2'].src = "img/bulletwhite.png";
}
animate("next",false);
                    },diff*options.speed+options.pause);
                }

options.auto表示它自动循环,dir是动作的方向,点击是用户是否点击它。

我的语法有问题吗?我用它运行了萤火虫,并没有抛出任何错误,它根本不起作用。任何建议都会有所帮助!

我应该提一下,bullet1的src从bulletwhite.png开始,然后我希望将它更改为bullet.png并将bullet2更改为bulletwhite.png。

2 个答案:

答案 0 :(得分:1)

document.images['bullet1'].src == "img/bulletwhite.png"

你确定满足这个条件吗?

通常,imageElement.src 属性包含src 属性绝对/已解决的版本。 (这也适用于href属性/属性。)

<img src="/images/img1.png">

...

document.images[0].src => "http://127.0.0.1/images/img1.png"

答案 1 :(得分:0)

根据你发布的代码,第二行有一个奇怪的分号,但也许这只是一个错字。

为什么不尝试使用JQuery attr()调用而不是使用document.images数组?

$('img[name="bullet1"]').attr('src', 'img/bullet.png');
相关问题