0

我正在尝试做的是从 0-3 随机生成每个页面加载的背景嵌入,如下所示。该文件位于body.index.html

<script type="text/javascript">
var music = [],
index = 0;

music[0] = "<embed name="myMusic" loop="false" hidden="true" src="music1.mp3"></embed>";
music[1] = "<embed name="myMusic" loop="false" hidden="true" src="music2.mp3"></embed>";
music[2] = "<embed name="myMusic" loop="false" hidden="true" src="music3.mp3"></embed>";
music[3] = "<embed name="myMusic" loop="false" hidden="true" src="music4.mp3"></embed>";

index = Math.floor(Math.random() * music.length);
document.write(music[index]);
</script>

问题是音乐不能在任何具有上述脚本的浏览器中播放。

4

1 回答 1

1

在双引号之前添加斜杠,如下所示:

music[0] = "<embed name=\"myMusic\" loop=\"false\" hidden=\"true\" src=\"music1.mp3\"></embed>";

或者像这样替换双引号:

music[0] = '<embed name="myMusic" loop="false" hidden="true" src="music1.mp3"></embed>';

完整代码:

<script type="text/javascript">
var music = [],
index = 0;

music[0] = '<embed name="myMusic" loop="false" hidden="true" src="music1.mp3"></embed>';
music[1] = '<embed name="myMusic" loop="false" hidden="true" src="music2.mp3"></embed>';
music[2] = '<embed name="myMusic" loop="false" hidden="true" src="music3.mp3"></embed>';
music[3] = '<embed name="myMusic" loop="false" hidden="true" src="music4.mp3"></embed>';

index = Math.floor(Math.random() * music.length);
document.write(music[index]);
</script>
于 2014-07-01T14:27:45.033 回答