標籤:style blog http color io 使用 java ar 檔案
1 <!-- 使用了HTML5的標籤<audio> 圖片播放按鈕增加了監聽監聽事件,控制音樂的播放暫停 2 使用了document的幾個重要函數: 3 1.createElement建立標籤對象, 4 2.getElementById擷取對象以及img對象的src屬性 5 3.document.body添加控制項appendChild()函數 6 瞭解document的強大。可以建立對象,操作body標籤等--> 7 <!DOCTYPE html> 8 <html> 9 <meta http-equiv="Content-Type" content="text/html;charset=utf-8">10 <body>11 <h3>音樂播放暫停樣本</h3>12 13 <p>點擊按鈕,播放、暫停音樂</p>14 15 <img id="image_button" src="source/pause.jpg" onclick="myFunction()"></button>16 17 <p id="txt">點擊按鈕播放音樂</p>18 19 <script type="text/javascript">20 var isPlay = false;21 var x = document.createElement("AUDIO");22 x.setAttribute("src", "source/music.mp3");23 document.body.appendChild(x);24 function myFunction()25 {26 if (isPlay == false) {27 x.play();28 document.getElementById("image_button").src="source/play.jpg";29 document.getElementById("txt").innerHTML = "<浮誇> —— eason";30 isPlay = true;31 }else{32 x.pause();33 document.getElementById("image_button").src="source/pause.jpg";34 document.getElementById("txt").innerHTML = "點擊按鈕播放音樂";35 isPlay = false;36 }37 }38 </script>39 40 </body>41 </html>
注意需要將pause.jpg,play.jpg,music.mp3放到source檔案夾下面。
檔案結構是{index.html,source[pause.jpg,play.jpg,music.mp3]} (index.html就是上面貼的代碼檔案,儲存成html格式的。)
HTML5下的Audio標籤控制項的歌曲播放暫停示範