在Javascript中切换开/关按钮
我卡住了。我有两个按钮,每个按钮都有一个功能,一个在单击时播放声音,另一个在单击时暂停声音。我想能够在它们之间切换,所以我只看到一个按钮在关闭时说“声音打开”,反之亦然。 问题是:我的Javascript技术在Javascript中切换开/关按钮,javascript,jquery,html,Javascript,Jquery,Html,我卡住了。我有两个按钮,每个按钮都有一个功能,一个在单击时播放声音,另一个在单击时暂停声音。我想能够在它们之间切换,所以我只看到一个按钮在关闭时说“声音打开”,反之亦然。 问题是:我的Javascript技术 <audio id="spacesound"> </audio> <button type="button" id="offbutton" class="offbutton" onclick="disableSound()">Sound off</
<audio id="spacesound"> </audio>
<button type="button" id="offbutton" class="offbutton" onclick="disableSound()">Sound off</button><br>
<button type="button" id="onbutton" class="onbutton" onclick="reenableSound()">Sound on</button>
可能会有一些CSS代码,但我更愿意用JS,谢谢 解决方案很简单,将实际状态“保存”在变量中
var isPlaying=false;
function playOrPause()
{
if(isPlaying)
{
myAudiothree.pause();
isPlaying=false;
}else
{
myAudiothree.play();
isPlaying=true;
}
}
现在只有一个函数而不是两个
编辑:
我为您创建了一个剪报,用于显示如何更改按钮文本
var isplay=false;
$(“#myButton”)。单击(函数(){
var$this=$(this);
如果(显示)
{
isplay=false;
$this.text('start');
}否则{
isplay=true;
$this.text('stop');
}
});代码>
播放
这就是我想要的!虽然我希望按钮上的文字也能改变,根据点击的内容,声音可以是关闭的,也可以是依赖的,但你知道我应该在那里做什么吗?这正是我想要的,谢谢你的帮助
var isPlaying=false;
function playOrPause()
{
if(isPlaying)
{
myAudiothree.pause();
isPlaying=false;
}else
{
myAudiothree.play();
isPlaying=true;
}
}