【问题标题】:How to play a sound on a checkbox in html css如何在html css中的复选框上播放声音
【发布时间】:2022-01-11 06:48:56
【问题描述】:
<audio id="myaudio" src="img/toggle.mp3" ></audio>
<div class="buttonpos">
<input class="toggle" onchange="myfunction" type="checkbox" id="switch">
<label onchange="document.getElementById('myaudio').play();" for="switch" class="switchLabel">
<span class="switchLabelBg"></span>
</label>
</div>
为什么这不起作用?当我点击我的复选框时,我正在尝试播放声音!
【问题讨论】:
标签:
html
jquery
css
audio
html5-audio
【解决方案1】:
玩过之后,document.getElementById('myaudio').play(); 实际上应该在 HTML 中的另一个 input 标记中,所以如果你将 HTML 更改为它应该可以工作:
<audio id="myaudio" src="img/toggle.mp3" ></audio>
<div class="buttonpos">
<input class="toggle" onchange="document.getElementById('myaudio').play();" type="checkbox" id="switch">
<label for="switch" class="switchLabel">
<span class="switchLabelBg"></span>
</label>
</div>