How to add music in JavaScript: first create a js sample file; then define an autoPlay method; finally introduce the music file through "myAuto.src='/media/v2/sss.mp3';" .
The operating environment of this article: windows7 system, javascript version 1.8.5, Dell G3 computer.
Adding music to the page through js
The code is as follows! Compatible with IE
// JavaScript Document function autoPlay(){//自动播放 var myAuto = document.getElementById('myaudio'); myAuto.src = '/media/v2/sss.mp3';//MP3路径 myAuto.play(); } function pausePlay(){//暂停播放 var myAuto = document.getElementById('myaudio'); myAuto.pause(); } function createAuto(){ var _id = $("#audio"); if (window.applicationCache) { _id.html('<audio id="myaudio" src="" controls="controls" loop="false" hidden="true"></audio>') } else { _id.html('<embed src="" id="myaudio" style="display: none;"></embed>'); } } 创建方式 createAuto();
[Recommended learning: javascript advanced tutorial]
The above is the detailed content of How to add music with JavaScript. For more information, please follow other related articles on the PHP Chinese website!