本文实例讲述了js实现可兼容IE、FF、Chrome、Opera及Safari的音乐播放器。分享给大家供大家参考。具体实现方法如下:
if(typeof(file)!='undefined'){
if(navigator.userAgent.indexOf("MSIE")>0){// IE
if(navigator.userAgent.indexOf("MSIE")>0){// IE
var player = document.createElement('bgsound');
player.id = id;
player.src = file['mp3'];
player.setAttribute('autostart','true');
if(loop){
player.setAttribute('loop','infinite');
}
document.body.appendChild(player);
}else{ // Other FF Chome Safari Opera
var player = document.createElement('au<a href="https://www.jb51.cc/tag/dio/" target="_blank" class="keywords">dio</a>');
player.id = id;
player.setAttribute('autoplay','autoplay');
if(loop){
player.setAttribute('loop','loop');
}
document.body.appendChild(player);
var mp3 = document.createElement('source');
mp3.src = file['mp3'];
mp3.type= 'au<a href="https://www.jb51.cc/tag/dio/" target="_blank" class="keywords">dio</a>/mpeg';
player.appendChild(mp3);
var ogg = document.createElement('source');
ogg.src = file['ogg'];
ogg.type= 'au<a href="https://www.jb51.cc/tag/dio/" target="_blank" class="keywords">dio</a>/ogg';
player.appendChild(ogg);
}
}
}
用法示例:
希望本文所述对大家的javascript程序设计有所帮助。
原文链接:https://www.f2er.com/js/55781.html