使用javascript在html中启动和停止嵌入音乐仅适用于IE

Start and stop for a embeded music in html using javascript works only in IE

本文关键字:音乐 IE 适用于 javascript html 启动 使用      更新时间:2023-09-26

我使用以下代码在网页中嵌入了一首音乐:

<embed src="upload/gnossiennes.mp3" autostart="true" loop="true" hidden="true"></embed>

然后我使用这个javascript代码来启动/停止它:

<script type="text/javascript">
    function play() {
        document.embeds[0].play();
    }
    function stop() {
        document.embeds[0].stop();
    }
</script> 
<span><a href="#" onClick="play();">START</a></span>
<span><a href="#" onClick="stop();">STOP</a></span>

这只适用于IE,但不适用于Chrome、FireFox和Opera。问题出在哪里?(这是页面中唯一的嵌入,所以我猜document.embeds[0]只是指这个嵌入,不应该是问题,除非这个数组在其他浏览器中不存在!

引用一个<embed>对于未来使用更多嵌入标记可能是致命的。如果您只想startstop,那么只需将其作为DOM元素创建并移除即可。这是一个类似的问题,有相同的解决方案