2013-04-21 53 views
0

的部分我有一個Vimeo的URL看起來像這樣:jQuery的替換URL

<iframe src="https://player.vimeo.com/video/57418480?byline=0&portrait=0&autoplay=1" width="620" height="350" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe> 

我想要做的就是把視頻ID,在這種情況下是57418480和的onclick更換。我會通過class屬性設置ID。例如:

<li><a href="#" class="57418481">Ethics in Investing</a></li> 

點擊上面的鏈接將只更換號碼ID,所以它會再看看這樣的:

<iframe src="https://player.vimeo.com/video/57418481?byline=0&portrait=0&autoplay=1" width="620" height="350" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe> 

回答

1
$('a').click(function() { 
    var newID = $(this).attr('class'); 
    var newURL = 'https://player.vimeo.com/video/' + newID + '?byline=0&portrait=0&autoplay=1'; 
    $('iframe').attr('src',newURL); 
    return false; 
}); 

http://jsfiddle.net/Khpd7/

2

使用數據鏈接在您的鏈接

<li><a href="#" data-url="https://player.vimeo.com/video/57418481?byline=0&portrait=0&autoplay=1" class="57418481">Ethics in Investing</a></li> 

onclick:

$('a').on('click',function(){ 
    $('iframe').attr('src',$(this).data('url')); 
}); 
+0

這實際上是接近你的答案problem.Thanks一個更好的方式,它的工作太棒了! :) – viablepath 2013-04-21 19:05:19