1 代碼很短,可以播放wav格式的小音效檔,請高手們指點指點,不要笑話小弟。sound是檔案名稱
- function Play(sound)
- {
- if(navigator.appName == "Microsoft Internet Explorer")
- {
- var snd = document.createElement("bgsound");
- document.getElementsByTagName("body")[0].appendChild(snd);
- snd.src = sound;
- }
- else
- {
- var obj = document.createElement("object");
- obj.width="0px";
- obj.height="0px";
- obj.type = "audio/x-wav";
- obj.data = sound;
- var body = document.getElementsByTagName("body")[0];
- body.appendChild(obj);
- }
-
- }
執行個體:
- <html>
- <head>
- <script>
- function Play(sound)
- {
- if(navigator.appName == "Microsoft Internet Explorer")
- {
- var snd = document.createElement("bgsound");
- document.getElementsByTagName("body")[0].appendChild(snd);
- snd.src = sound;
- }
- else
- {
- var obj = document.createElement("object");
- obj.width="0px";
- obj.height="0px";
- obj.type = "audio/x-wav";
- obj.data = sound;
- var body = document.getElementsByTagName("body")[0];
- body.appendChild(obj);
- }
-
- }
- </script>
- </head>
- <body>
- <input type="button" id="test" onclick="Play('gold.wav');" value="測試聲音">
- </body>
- </html>