2013-08-17 210 views
0

我試圖做出一個簡單的代碼,如果用戶點擊按鈕聲音播放。當按下按鈕時播放聲音

這裏是代碼:

<script> 
function play(){ 
$("body").append("<embed src='play.mp3' autostart='true' loop='false' width='2' height='0'></embed>"); 
} 
</script> 

<form action="" method="post"> 
<input type="button" onclick="play()" value="click"> 
</form> 

回答

2

試試這個。應該管用。

<!DOCTYPE html> 
<html> 
<head> 
<script language="javascript"> 
function playIt() 
{ 
    document.getElementById("embed").innerHTML="<embed src='play.mp3' autostart=true loop=false volume=100 hidden=true>"; 
    return true; 
} 
</script> 
</head> 
<body> 
    <form action="" method="post"> 
     <button type="button" onclick="playIt()">play</button> 
     <div id="embed"></div> 
    </form> 
</body> 
</html>