案例:javascript案例     状态:可编辑再运行    进入竖版
 运行结果 
x
 
1
<!DOCTYPE html>
2
<html>
3
<body>
4
5
<h3>演示如何创建 SOURCE 元素</h3>
6
7
<p>点击按钮来创建两个 SOURCE 元素,并把它们添加到 AUDIO 元素。</p>
8
9
<audio controls id="myAudio" autoplay>
10
Your browser does not support the audio tag.
11
</audio><br>
12
13
<p id="demo"></p>
14
15
<button onclick="myFunction()">试一下</button>
16
17
<script>
18
function myFunction()
19
{
20
var x = document.createElement("SOURCE");
21
x.setAttribute("src", "/img/horse.mp3");
22
x.setAttribute("type", "audio/mpeg");
23
document.getElementById("myAudio").appendChild(x);
24
25
var y = document.createElement("SOURCE");
26
y.setAttribute("src", "/img/horse.ogg");
27
y.setAttribute("type", "audio/ogg");
28
document.getElementById("myAudio").appendChild(y);
29
document.getElementById("demo").innerHTML = "音频播放器可以工作了,点击播放按钮会听到一段声音。";
30
}
31
</script>
32
33
</body>
34
</html>
35