我是jQuery / JS的新手并尝试自动点击生成的iframe以便播放视频,因为在iframes网址设置时不会认为自动播放有效,是否有可能并且我可能会犯错?
小提琴:https://jsfiddle.net/p7v34x57/1/
JS:
$.each($('iframe[src*="facebook.com/plugins/video"]'), function() {
var getId = $(this).attr('src');
var player = $('<div class="video-player">');
var id = fbvideoID(getId);
player.attr('data-id', id);
player.html(videoThumbFB(id));
player.on('click', videoIframeFB);
var videoContainer = $('<div class="video-container">');
videoContainer.append(player);
$(this).replaceWith(videoContainer);
});
function fbvideoID(frame) {
var myRegexp = /2F(\d+)%/g;
var match = myRegexp.exec(frame);
return match[1];
}
function videoThumbFB(id) {
return '<img class="facebook-thumb" src="//graph.facebook.com/' + id + '/picture"><div class="play-button"></div>';
}
function videoIframeFB() {
var wrap = $('<iframe>');
wrap.attr("src", "//www.facebook.com/video/embed?video_id=" + $(this).attr('data-id') + "&autoplay=1");
wrap.attr("frameborder", "0");
wrap.addClass("facebook-iframe");
$(this).empty();
$(this).append(wrap);
videoClickFB();
}
function videoClickFB(){
$('.facebook-iframe').click();
}