gpt4 book ai didi

jquery - 将值加载到jQuery插件中的按钮

转载 作者:行者123 更新时间:2023-12-03 06:03:34 24 4
gpt4 key购买 nike

我正在使用在这里找到的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视频,而无需刷新页面。

我不知道该怎么做。我已经花了数小时试图找出答案,但没有任何运气。任何帮助将是极大的。赞赏

最佳答案

的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

===更新===

将事件处理程序调用放入按钮的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>

另请参阅 the updated example

===更新===

也许这个免费的;-)示例可以帮助您:

的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);
}
}

另请参阅此 updated jsfiddle

关于jquery - 将值加载到jQuery插件中的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9307095/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com