播放和与同图的onclick暂停 [英] Play and Pause with onclick on the same Img
本文介绍了播放和与同图的onclick暂停的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面的脚本正在播放的音效档,当我在IMG(的onclick)点击。我如何通过点击同一IMG暂停?从来就试图audio.pause(),但它不会致使工作。
功能播放(){
VAR音频=的document.getElementById(myaudio);
audio.style.display =块;
audio.play();
}< A HREF =#的onclick =播放()>< IMG SRC =BILDER / play2.png>< / A>
解决方案
您应该重命名功能 audioHandler()
例如将处理是否播放或暂停音频。
创建布尔要记住,如果您的音频被打或者是在暂停。
//初始状态:音频不打
VAR状态= FALSE;
VAR音频=的document.getElementById(myaudio);功能audioHandler(){
如果(状态== ||假audio.paused){
audio.play();
状态= TRUE;
}其他{
audio.pause();
状态= FALSE;
}
}
The following script is playing a soundfile when i click on a img (onclick). How do i pause it by clicking on the same img? I´ve tried audio.pause(), but it won´t work.
function play(){
var audio = document.getElementById("myaudio");
audio.style.display="block";
audio.play();
}
<a href="#" onclick="play()"><img src="Bilder/play2.png"></a>
解决方案
You should rename your function to audioHandler()
for example which will handle whether to play or pause your audio.
Create a boolean to remember if your audio was playing or was on pause.
//initial status: audio is not playing
var status = false;
var audio = document.getElementById("myaudio");
function audioHandler(){
if(status == false || audio.paused){
audio.play();
status = true;
}else{
audio.pause();
status = false;
}
}
这篇关于播放和与同图的onclick暂停的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文