2011-12-12 31 views
1

我正在寻找一种方式来找到这样的“颜色框负荷”级链接...使用jquery如何更改具有某个CSS类的元素的href值?

<a class="colorbox-load" href="http://www.youtube.com/watch?v=hYVGKdop63Y?fs=1&amp;width=640&amp;height=480&amp;hl=en_US1&amp;iframe=true&amp;rel=0">Kendra Grittani on stage at Koerner Hall (broken)</a> 

...和使用jquery我想替换每个实例中的href标签“ youtube.com/watch?v=“与”youtube.com/v/“。

这是我迄今为止,但我不知道如何执行查找和替换。

$("a.colorbox-load").attr("href", "http://www.youtube.com") { 
    var text = $(this).text(); 
    text = text.replace("youtube.com/watch?v=", "youtube.com/v/"); 
    $(this).text(text); 
}); 

回答

7

使用简单字符串replace()

$("a.colorbox-load").each(function(){ 
    var newUrl = $(this).attr('href').replace('youtube.com/watch?v=', 'youtube.com/v/'); 
    $(this).attr('href', newUrl); 
}); 

UPDATE

jsFiddle为您推荐的一个例子。

+0

谢谢,这是有道理的。但是,它不适合我。我很担心这可能是因为我的一些事情发生了,所以我会多玩一会儿。 –

+0

用一个例子更新了我的答案。试试吧 – Stefan

+0

是的,这是我的错。这是最终为我工作的。 (函数($){ \t \t $( 'a.colorbox负荷')。每个(函数(){ \t变种NEWURL = $(本).attr( 'href' 属性)。代替('的YouTube (jQuery);}};}};}}; })(jQuery);/var /'; –

相关问题