我有这个代码,我不知道为什么它不起作用
<html>
<head>
<title>Test</title>
<script type="text/javascript">
function StopBackgroundMusic()
{
var MusicObj = document.getElementById("BackgroundMusic");
MusicObj.data = "http://www.oreillynet.com/examples/oreilly/digitalmedia/2005/02/ableton_intro_0205_gtr.mp3";
MusicObj.autoplay = "false";
MusicObj.autostart = "false";
return MusicObj;
}
</script>
</head>
<body>
<OBJECT ID="BackgroundMusic" data="http://www.oreillynet.com/examples/oreilly/digitalmedia/2005/02/ableton_intro_0205_gtr.mp3" TYPE="audio/mpeg" height="0" width="0">
<PARAM NAME="autostart" VALUE="true">
<PARAM NAME="autoplay" VALUE="true">
</OBJECT>
<form>
<input type='button' onclick='StopBackgroundMusic()' value='Stop music'/>
</form>
</body>
</html>
答案 0 :(得分:3)
尝试将数据设置为null
,而不是再次设置音乐网址。
另一种方法是在按停止按钮时删除对象节点:
function StopBackgroundMusic()
{
var MusicObj = document.getElementById("BackgroundMusic");
MusicObj.parentNode.removeChild(MusicObj);
}
答案 1 :(得分:0)
元素“BackgroundMusic”是否封装在表单元素中?如果是这样的话,请尝试类似这样的事情,
document.getElementById("[formID].BackgroundMusic");
如果你使用表单标签,请给它一个id,
<form id="music">