本文目錄一覽:
javascript播放聲音的函數是什麼?
可以用bgsound標籤。
html
head
script
function init(){
if(判斷條件有消息){
document.getElementById(“sound”).src=”sound.wav”;
或者document.all.sound.src=”sound.wav”;
或者document.getElementsByName(“*”).sound.src=”sound.wav”;
}
}
/script
/head
body
bgsound id=”sound”
/body
/html
這樣就可以播放了默認情況下不會循環。如果使用embed也可以使用上面的想法
embed id=”sound” src=”msg.wav” width=”42″ height=”35″ hidden=”true” autostart=”false”/embed
如果是ie就直接寫document.getElementById(“sound”).play()
如果是mozilla-firefox就就把document.getElementById(“sound”).autostart=true;
js 在網頁內播放聲音文件,該怎麼解決
通過html5的audio標籤就可以實現了,不需要用到js
audio src=”song.ogg” controls=”controls”
/audio
Javascript 如何實現播放聲音?
在頁面body里加上
bgsound id=”snd” loop=”0″ src=””
然後再head/head之間加上
script
function playSound(src){
var _s = document.getElementById(‘snd’);
if(src!=” typeof src!=undefined){
_s.src = src;
}
}
/script
調用時,比如用按鈕調用:
input type=”button” value=”聲音” onclick=”playSound(‘這裡填寫你的聲音文件路徑’)”
聲音文件盡量要小,幾十K到wav文件即可
js實現網頁關閉聲音
你是說關閉瀏覽器內任意頁面上任意文件產生的聲音的話,你還是直接關閉音響算了。js沒這個能力。
原創文章,作者:IKIWK,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/317942.html