我使用的是此网站上发布的以下脚本:http://www.javascriptkit.com/script/...oundlink.shtml
效果很好,但我希望扩展此脚本并使用复选框将声音静音。此复选框具有id:sound。如果选中,我想听到声音,如果未选中,则不会听到声音。
这是我目前为复选框提供的代码: 代码:
function playSound() {
if (document.getElementById('sound').checked){
-something needs to be added here to make it work?-
}
}
任何人都知道如何使这项工作?
非常感谢
答案 0 :(得分:1)
function muteIt(box){ if(clicksound){ clicksound.muted = box.checked?true:false; } if(mouseoversound){ mouseoversound.muted = box.checked?true:false; } }
答案 1 :(得分:0)
<script>
// Mouseover/ Click sound effect- by JavaScript Kit (www.javascriptkit.com)
// Visit JavaScript Kit at http://www.javascriptkit.com/ for full source code
//** Usage: Instantiate script by calling: var uniquevar=createsoundbite("soundfile1", "fallbackfile2", "fallebacksound3", etc)
//** Call: uniquevar.playclip() to play sound
var html5_audiotypes={ //define list of audio file extensions and their associated audio types. Add to it if your specified audio file isn't on this list:
"mp3": "audio/mpeg",
"mp4": "audio/mp4",
"ogg": "audio/ogg",
"wav": "audio/wav"
}
function createsoundbite(sound){
var html5audio=document.createElement('audio')
if (html5audio.canPlayType){ //check support for HTML5 audio
for (var i=0; i<arguments.length; i++){
var sourceel=document.createElement('source')
sourceel.setAttribute('src', arguments[i])
if (arguments[i].match(/\.(\w+)$/i))
sourceel.setAttribute('type', html5_audiotypes[RegExp.$1])
html5audio.appendChild(sourceel)
}
html5audio.load()
html5audio.playclip=function(){
if (document.getElementById('sound').checked){
html5audio.pause()
html5audio.currentTime=0
html5audio.play()
}
}
return html5audio
}
else{
return {playclip:function(){throw new Error("Your browser doesn't support HTML5 audio unfortunately")}}
}
}
//Initialize two sound clips with 1 fallback file each:
var mouseoversound=createsoundbite("whistle.ogg", "whistle.mp3")
var clicksound=createsoundbite("click.ogg", "click.mp3")
</script>
因此,它会检查是否选中了该框。如果是的话,它就会播放。如果没有,那么它将不会播放。