检测和更改mp3链接javaScript

时间:2011-12-21 07:30:47

标签: javascript audio

我需要帮助改变

<a>'s with a href ending in the .mp3 extension from <a href="audioLink"></a> 

<embed type="application/x-shockwave-flash" flashvars="audioUrl=audioLink.mp3" src="http://www.google.com/reader/ui/3523697345-audio-player.swf" width="500" height="27" quality="best"></embed>

使用JavaScript

2 个答案:

答案 0 :(得分:3)

通过 - jQuery

$("a").each(function(){ 
   var link = $(this).attr("href");
   if(~link.indexOf('.mp3')) {
           $("<embed type='application/x-shockwave-flash' flashvars='audioUrl="+link+"' src='http://www.google.com/reader/ui/3523697345-audio-player.swf' width='500' height='27' quality='best'></embed>").insertAfter(this);
            $(this).remove();
    }

 })

或纯Javascript

(function( l ){ 
    for( var i=l.length; i--;)
        var link = l[i];
        if(~link.href.indexOf('.mp3')) {
           var p = link.parentNode,
               embed = document.createElement("embed"),
               attr = {  type:'application/x-shockwave-flash',
                         flashvars: 'audioUrl='+link.href,
                         src:'http://www.google.com/reader/ui/3523697345-audio-player.swf',
                         width:'500',
                         height:'27',
                         quality:'best'}

           for(var j in attr)
               embed[j]=attr[j]
           p.insertBefore(embed,link);
           p.removeChild(link);

        }


})( document.getElementsByTagName( "a" ) )

答案 1 :(得分:0)

以下是示例解决方案:

var links = $(document.body).find('a'), embeded;
for (var i = 0; i < links.length; i+=1) {
    if ($(links[i]).attr('href').indexOf('.mp3') >= 0) {
        embeded = $('<embed type="application/x-shockwave-flash" flashvars="audioUrl='+ $(links[i]).attr('href') +'" src="http://www.google.com/reader/ui/3523697345-audio-player.swf" width="500" height="27" quality="best"></embed>');
        embeded.insertBefore($(links[i]));
        $(links[i]).remove();
    }
}

您可以在此处看到它:http://jsfiddle.net/4JqaA/