上代码
( function($){
$.fn.audio = function(musicURI){
$(this).append(" <i class='fa fa-play'></i>");
$(this).click(function(){
if( element(musicURI)) {
$(this).children('.fa').removeClass("fa-play");
$(this).children('.fa').addClass("fa-pause");
} else {
$(this).children('.fa').removeClass("fa-pause");
$(this).children('.fa').addClass("fa-play");
}
});
debug('Audio');
};
function debug(obj) {
if(window.console && window.console.log) {
window.console.log("%c jQuery Plugin " + obj, 'background-image:url(http://cc-cdn.b0.upaiyun.com/static/console.jpg); line-height: 10px; background-repeat: no-repeat; padding-left: 40px; background-position: left center; color: #F217B9; ');
}
};
function element(uri) {
if(! $("audio").length > 0)
{
var element = document.createElement("audio");
element.setAttribute('src', uri);
element.setAttribute('preload',true);
element.setAttribute('loop',true);
element.setAttribute('volume', 0.1);
}
//element.play();
if(element.paused)
{
element.play();
return true;
}
else
{
element.pause();
return false;
}
};
})(jQuery);
$("#audio").audio("http://m1.music.126.net/_-Or3YYe0bjwEXkiz-pWHw==/7733964790811266.mp3");
多次点击会有多个声音出来, 怎么避免
audio
重复创建? 谢谢