【发布时间】:2012-02-12 17:37:50
【问题描述】:
我已经有一个插件可以在我的网站上嵌入视频,名为 oembed。我需要的是一个 javascript 代码,当单击图像时,视频播放器会出现并播放,它将 youtube iframe 或其他嵌入转换为视频的默认图像。 Google+ 和 facebook 已经实现了这个想法。请告诉我如何做到这一点。
这是我的 youtube iframe 的示例:
<iframe width="267" height="200" src="http://www.youtube.com/embed/YOUTUBEID;feature=oembed" frameborder="0" allowfullscreen=""></iframe>
我找到了两个代码,修改成这样:
var RE = /embed\/([a-zA-Z0-9_-]{11})/;
jQuery( "iframe" ).each(function(){
var id = ( this.src.match( RE ) || [] )[1];
if( id ) {
jQuery( this ).replaceWith(
'<img width="420" height="215" src="http://i1.ytimg.com/vi/'+id+'/hqdefault.jpg"'+
' class="youtube" />' );
}
});
var youtubeIFRAME = '<p class="close">CLOSE</span><iframe data-address="$1" src="http://www.youtube.com/embed/$1?rel=0" frameborder="0" allowfullscreen></iframe>';
jQuery("img.youtube").click(function(){
var $this = $(this);
$this.replaceWith('<iframe width="420" height="315" src="http://www.youtube.com/embed/$1?rel=0" frameborder="0" allowfullscreen></iframe>'
.replace("$1", $this.attr("data-address")));
});
问题现在是嵌入工作,但捕捉视频 ID 时出现问题。
【问题讨论】:
标签: java javascript youtube embed thumbnails