<html>
<head>
<title>Greeting - Basic</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<script language=javascript>
var i=0;
function plays(){
if(i==0){document.a.play();i++;}
else { document.a.pause();i--;}
}
</script>
</head>
<body>
<embed name=a src="c:/a.mp3" loop=false autostart=false mastersound hidden=true width=0 height=0></embed>
<a href="#" onClick="plays()"> 播放/暂停</a> <br><br>
</body> </html> 上面程序的功能是点击"播放/暂停"按钮会听到一段声音,但我有n个"播放/暂停"按钮,可否把plays方法写成一个通法?

解决方案 »

  1.   

    我是初学java,javascript学过两天好像能看懂错了就当没说
    function plays()
    {
        if(i==0)
        {
             document.a.play();
             documnet.b.play();
             document.x.play();
             i++;
         }
         else
        {
            全停止,照着上面抄一遍
         }
    }
    似乎就行
      

  2.   

    还可以更好,楼主根据需要自己改吧isPlay这个属性自己加的你可以随便改什么都行,只要不覆盖原有属性就行了。<html> 
    <head> 
    <title></title> 
    <script language=javascript>
    function play(obj,isPause){
    var emb=document.getElementById(obj)
    if(emb.isPlay==true||isPause==true){
    emb.isPlay=false;
    emb.pause();
    }else {
    emb.isPlay=true;
    emb.play();

    }
    function pauseAll(){
    play('p1',true);
    play('p2',true);
    play('p2',true);
    }
    </script> 
    </head> 
    <body> 
    <embed  name=p1   src= "E:\music\soft\NewMailMsg.mp3" loop=false autostart=false mastersound hidden=true width=0 height=0> </embed>
    <embed  name=p2  src= "E:\TDDOWNLOAD\47587.mp3" loop=false autostart=false mastersound hidden=true width=0 height=0> </embed> 
    <embed  name=p3  src= "E:\TDDOWNLOAD\310984657.mp3" loop=false autostart=false mastersound hidden=true width=0 height=0> </embed> 
    <a href= "# " onClick="pauseAll();play('p1')">   播放/暂停 </a>
    <a href= "# " onClick="pauseAll();play('p2')">   播放/暂停 </a>
    <a href= "# " onClick="pauseAll();play('p3')">   播放/暂停 </a>
    </body> 
    </html>   
      

  3.   

    功能有点不对,我要求"播放"和"暂停"按钮合在一起, sunyujia只实现了播放功能,能再帮我改改吗,谢谢!!!