使用 javascript 在 html 中开始和停止嵌入的音乐仅适用于 IE

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9750010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 07:42:52  来源:igfitidea点击:

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

javascripthtmlembed

提问by Saeid Yazdani

I have embeded a music in a webpage using this code:

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

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

Then I am using this javascript code to start/stop it:

然后我使用这个 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>

This works only in IE but not in Chrome, FireFox and Opera. What can be the problem? (this is the only embed in the page so I guess document.embeds[0]is only referring to this embed and shouldnt be the problem, unless this array does not exist in other browsers!

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

采纳答案by jmishra

Well referencing one <embed>could be fatal for prospective uses of more embed tags. If you simply want to startand stopit then simply create and remove it as a DOM element. Here was a similar questionand had the same solution

<embed>对于更多嵌入标签的预期用途,引用一个可能是致命的。如果你只是想startstop它再简单地创建和删除它作为一个DOM元素。这是一个类似的问题并且有相同的解决方案