用javascript更改图像

时间:2013-09-17 18:36:46

标签: javascript

我使用jquery mobile制作应用。我在页面加载时播放声音文件并显示图像以在单击时关闭声音文件。一切正常,但我只能在索引页面上更改图像。我可以关闭任何页面上的声音文件,但图像不会在任何其他页面上更改。请帮助=)< 3

<script type="text/javascript">
var newsrc = "soundOff.png";

function changeImage() {
    var sample = document.getElementById("foobar");
    sample.pause();

    if ( newsrc == "soundOff.png" ) {
        document.images["sound"].src = "/img/soundOff.png";
        document.images["sound"].alt = "Sound Off";
        newsrc  = "soundOn.png";
    }
    else {
        var sample = document.getElementById("foobar");
        sample.play();
        document.images["sound"].src = "/img/soundOn.png";
        document.images["sound"].alt = "Sound On";
        newsrc  = "soundOff.png";
    }
}
var sample = document.getElementById("foobar");
sample.play();

1 个答案:

答案 0 :(得分:1)

document.images["sound"].src = "/img/soundOff.png";

应该是这样的:

document.getElementById('your_id').src = "/img/soundOff.png";

你的道路对我来说似乎有点奇怪。检查一下