【发布时间】:2015-02-17 17:58:54
【问题描述】:
我有一个 wordpress 页面,我希望在单击图像时播放声音。我从本教程中获得了代码:
http://iwebfaq.org/site/iWeb_Sound_effects.html
基本上,我编辑了页面并在文本编辑器中添加了这个:
<script>
// Mouseover/ Click sound effect- by JavaScript Kit (www.javascriptkit.com)
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(){
html5audio.pause()
html5audio.currentTime=0
html5audio.play()
}
return html5audio
}
else{
return {playclip:function(){throw new Error("Your browser doesn't support HTML5 audio unfortunately")}}
}
}
var mouseoversound1=createsoundbite("http://mywebsite.com/wp-content/uploads/2015/02/island_music_x.mp3")
var clicksound1=createsoundbite("http://mywebsite.com/wp-content/uploads/2015/02/island_music_x.mp3")
</script>
然后我添加这个:
<a nohref onclick="clicksound1.playclip()"><img src="http://mywebsite.com/wp-content/uploads/2015/02/ananas.png" style="width:126px;height126px;border:0px;"></a>
我做错了什么?当我应用它时没有任何反应,图像显示但点击它绝对没有任何效果
【问题讨论】:
标签: javascript wordpress html5-audio