我正在使用Jquery youtube播放器插件:http://badsyntax.github.com/jquery-youtube-player/
该插件允许您为要播放的YouTube视频和YouTube视频ID指定标题。以下是代码片段:
var config = {
repeatPlaylist: 1,
showTime: 1,
height: 356,
toolbar: 'play,prev,next,shuffle,repeat,mute', // comma separated list of toolbar buttons
// Custom playlist
playlist: {
title: 'Random videos',
videos: [
{ id: 'youtube video id goes here', title: 'title goes here' },
]
}
};
我想创建一个按钮并指定youtube id和视频标题。按下时,我希望使用这些值来使用上方的jQuery插件加载youtube视频,而不刷新页面。
我不知道如何做到这一点。我花了好几个小时试图弄清楚它没有任何运气。任何帮助都会很大。理解
答案 0 :(得分:1)
HTML:
<div class="youtube-player"></div> <!-- this element will be replaced -->
<button type="button" id="videoBtn1">video 1</button>
<button type="button" id="videoBtn2">video 2</button>
JS:
function loadYouTube(id, title) {
// your config with the parameters id and title
var config = {
repeatPlaylist: 1,
showTime: 1,
height: 356,
toolbar: 'play,prev,next,shuffle,repeat,mute',
// Custom playlist
playlist: {
title: 'Random videos',
videos: [{ id: id, title: title }]
}
};
// replace the html element with an empty you-tube plugin html code
$('.youtube-player').replaceWith($('<div class="youtube-player"><div class="youtube-player-video"><div class="youtube-player-object">You need Flash player 8+ and JavaScript enabled to view this video.</div></div></div>'));
// start youtube-plugin
$('.youtube-player').player(config);
}
// click handlers
$('#videoBtn1').click(function() {
loadYouTube('3qQWibGlfb0', 'title 1 goes here');
});
$('#videoBtn2').click(function() {
loadYouTube('b8MhLvjoBTs', 'title 2 goes here');
});
另见this example。
=== UPDATE ===
将事件处理程序调用放入按钮的onclick属性中,并在javascript中删除它们:
<button type="button" id="videoBtn1" onclick="loadYouTube('3qQWibGlfb0', 'title 1 goes here');">video 1</button>
<button type="button" id="videoBtn2" onclick="loadYouTube('b8MhLvjoBTs', 'title 2 goes here');">video 2</button>
=== UPDATE ===
也许这个免费;-)例子可以帮助你:
HTML:
<div id="youtube-player">
<div class="youtube-player-video">
<div class="youtube-player-object">
You need Flash player 8+ and JavaScript enabled to view this video.
</div>
</div>
</div>
<button type="button" id="videoBtn1" onclick="loadYouTube('3qQWibGlfb0', 'title 1 goes here');">video 1</button>
<button type="button" id="videoBtn2" onclick="loadYouTube('b8MhLvjoBTs', 'title 2 goes here');">video 2</button>
JS:
var player = null;
var playlist = {
title: 'Random videos',
videos: []
};
function loadYouTube(id, title) {
// check if player isn't already loaded
if (typeof $('#youtube-player').data('jquery-youtube-player') == 'undefined') {
// add video to playlist
playlist.videos.push({id: id, title: title});
// load player
player = $('#youtube-player')
.show()
.player({
repeatPlaylist: 1,
showTime: 1,
height: 356,
toolbar: 'play,prev,next,shuffle,repeat,mute',
playlist: playlist
});
}
// if player is active
else {
var bFound = false;
// search if video is already in playlist
for (var i = 0; i < playlist.videos.length; i++) {
if (playlist.videos[i].id == id) {
bFound = true;
break;
}
}
if (!bFound) {
// add video to playlist
playlist.videos.push({id: id, title: title});
// load updated playlist
player.player('loadPlaylist', playlist);
}
// show current video
player.player('cueVideo', id);
}
}
答案 1 :(得分:0)
你可以在这里使用一些不同的方法。但主要的主题是每个按钮都使用一个标识符来告诉脚本该做什么。
您可以为每个按钮构建一个配置对象:
var btn_config = { myBtn1: { youtubeId: 1, title: 'abc' }, myBtn2: { youtubeId: 2, title: 'abc' }}; // And have each btn html element have the same id. And get the config like: $('a').click(function (e) { var self = $(this), currentConfig = btn_config[self.attr('id')]; // currentConfig.youtubeId });
您还可以使用html5数据属性(这些是jQuery提取):
// A html element with data-attribute
<a href="#" data-youtube-id="1" data-youtube-title="some title">Button</a>
// And to pick it up with jquery
$('a').click(function (e) {
var self = $(this),
youtubeId = self.data('youtubeId');
});
我建议使用数据属性方式,因为它提供了一些更简单的方法来添加新按钮。
这是一个快速的jsfiddle:http://jsfiddle.net/68csa/
..弗雷德里克