【问题标题】:Button that loads values into jQuery plugin将值加载到 jQuery 插件中的按钮
【发布时间】:2012-03-07 14:29:52
【问题描述】:

我正在使用此处找到的 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 视频而不刷新页面

我不知道该怎么做。我花了几个小时试图弄清楚没有任何运气。任何帮助都会很大。赞赏

【问题讨论】:

    标签: jquery ajax api youtube


    【解决方案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

    === 更新 ===

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

    【讨论】:

    • 感谢您的回复。对此,我真的非常感激。我已经坚持了好几天了,你的代码示例会对我有很大帮助。是否可以将手柄移到 JS 之外?并使用 html 输入 youtube id 和标题?
    • 谢谢!你以前用过这个插件吗?
    • 我在上面的解决方案中遇到了问题。我想要它,所以当按下按钮 #1 时,视频 #1 会添加到播放列表中。如果按下按钮 #2,视频也会被添加到播放列表中。然后用户可以选择在视频之间来回移动并使用插件的其他一些功能(随机播放、重复播放)。类似于jsfiddle.net/d4DT2/2,您的播放列表中有多个视频,可以切换到下一个视频、重复播放、随机播放等。如果我要求太多,请告诉我。
    • 有点像添加到播放列表中播放的视频队列。
    • 非常感谢您抽出宝贵时间帮助我!我真的很感同身受!再次感谢!!!
    【解决方案2】:

    您可以在这里使用一些不同的方法。但主要主题是让每个按钮使用一个标识符来告诉脚本该做什么。

    您可以为每个按钮构建一个配置对象:

     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/

    ..弗雷德里克

    【讨论】:

    • 感谢您的回复。我将如何在上面的 jQuery 代码中插入 ID 和标题?抱歉,我真的很陌生。
    • 如果你看我的第二个建议,你可以将 id 和 title 直接添加到 html 元素中。
    猜你喜欢
    • 1970-01-01
    • 2013-04-07
    • 1970-01-01
    • 2015-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多