播放随机音乐 [英] Play Random Music
本文介绍了播放随机音乐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在我的网站播放随机音轨..没有任何前进/后退控制..
我使用的代码是..
I want to play random sound tracks on my website.. Without any forward/rewind control.. The code i am using for it is..
<script type="text/javascript">
var a = Math.random()*2;
a=Math.floor(a);
if(a==1)
{alert(a); document.getElementById('soundtrack').innerHTML="<audio id='background_audio1' loop autoplay><source src='6.ogg' type='audio/ogg'>Your browser does not support the audio element.</audio>";}
if(a==0)
{alert("hello");document.getElementById('soundtrack').innerHTML="<audio id='background_audio1' loop autoplay><source src='5.ogg' type='audio/ogg'>Your browser does not support the audio element.</audio>";}
</script>
<div id="soundtrack">
</div>
但是这段代码不会播放任何东西..(警报进行的非常顺利)
什么我应该怎么做?
But this code is not playing anything.. (alert is going very well) What should i do to???
推荐答案
实际上,当您运行脚本时,div未被初始化。你需要把它放在onload哈勒。这个例子适用于我:
Actually the div is not initialized when you run the script. You need to place it in onload hadler. This example works for me:
<script type="text/javascript">
function play()
{
var a = Math.random()*2;
a=Math.floor(a);
if(a==1)
{
alert(a);
document.getElementById('soundtrack').innerHTML="<audio id='background_audio1' loop autoplay><source src='1.mp3' type='audio/ogg'>Your browser does not support the audio element.</audio>";
}
if(a==0)
{
alert(document.getElementById('soundtrack'))
document.getElementById('soundtrack').innerHTML="<audio id='background_audio1' loop autoplay><source src='0.mp3' type='audio/ogg'>Your browser does not support the audio element.</audio>";
}
}
</script>
<body onload="play()">;
<div id="soundtrack">
</div>
</body>
这篇关于播放随机音乐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文