简体   繁体   English

用JavaScript更改图像src不起作用

[英]change image src with javascript not working

Here is my code 这是我的代码

function playPause(){
    movie = document.getElementById('movieEMBED');
    icon = document.getElementById('playPauseIcon');
    var isPlaying;
    isPlaying = movie.GetRate();
    if (!isPlaying){
        movie.Play();
        icon.setAttribute('src', "images/pausebutton.png");
    }
    else {
        movie.Stop();
        icon.setAttribute('src', "images/playbutton.png");
    }
}

in the html: 在html中:

<input type="image" id="playPauseIcon" src="images/playbutton.png" width=30px onClick='playPause()'></input>

Any ideas as to why the picture does not switch between the pause and play button when I click the button? 关于为什么单击按钮后图片无法在暂停和播放按钮之间切换的任何想法?

I'm guessing it's because your movie is playing. 我猜是因为您的电影正在播放。 The else of the if block sets the src to the same value: if块的else将src设置为相同的值:

if (!isPlaying){
    movie.Play();
    icon.setAttribute('src', "images/pausebutton.png");
}
else {
    movie.Stop();
    icon.setAttribute('src', "images/playbutton.png"); // <---- no change
}

Just to be sure that your code is right, which it appears to be, I would change the function to this: 为了确保您的代码正确无误,我将函数更改为:

function playPause(){
    var icon = document.getElementById('playPauseIcon');
    icon.setAttribute('src', "images/pausebutton.png");
}

and then slowly add in your movie code 然后慢慢添加影片代码

As far as I can see, your code is fine. 据我所知,您的代码很好。 Are you sure movie.GetRate() returns different values and you're passing by both paths of the 'if' correctly? 您确定movie.GetRate()返回不同的值,并且您正确地通过了“ if”的两个路径吗? I would think movie.GetRate() is returning always the same value so you never set the src attribute to a different image. 我认为movie.GetRate()总是返回相同的值,因此您永远不会将src属性设置为其他图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM