jquery - 使用jquery动态嵌入youtube视频

标签 jquery flash youtube embed youtube-api

我正在尝试检索用户的 YouTube 视频列表并使用 jQuery 将它们嵌入到页面中。我的代码看起来像这样:

  $(document).ready(function() {  

  //some variables  
  var fl_obj_template = $('<object width="260" height="140">' +   
                          '<param name="movie" value=""></param>' +  
                          '<param name="allowFullScreen" value="true"></param>' +  
                          '<param name="allowscriptaccess" value="always"></param>' +  
                          '<embed src="" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="260" height="140"></embed>' +  
                          '</object>');  

  var video_elm_arr = $('.video');  


  //hide videos until ready
  $('.video').addClass('hidden');

  //pull video data from youtube
  $.ajax({
    url: 'http://gdata.youtube.com/feeds/api/users/username/uploads?alt=json',
    dataType: 'jsonp',
    success: function(data) {
      $.each(data.feed.entry, function(i,item){
        //only take the first 7 videos
        if(i > 6)
          return;

        //give the video element a flash object
        var cur_flash_obj = fl_obj_template;

        //assign title
        $(video_elm_arr[i]).find('.video_title').html(item.title.$t);

        //clean url
        var video_url = item.media$group.media$content[0].url;
        var index = video_url.indexOf("?");
          if (index > 0)
            video_url = video_url.substring(0, index);

        //and asign it to the player's parameters
        $(cur_flash_obj).find('object param[name="movie"]').attr('value', video_url);
        $(cur_flash_obj).find('object embed').attr('src', video_url);

        //alert(cur_flash_obj);

        //insert flash object in video element
        $(video_elm_arr[i]).append(cur_flash_obj);

        //and show
        $(video_elm_arr[i]).removeClass('hidden');
      });
    }
  });
});  

(当然“用户名”是实际的用户名)。

视频标题显示正确,但没有视频显示。给出了什么?

目标 html 如下所示:

<div id="top_row_center" class="video_center video">
  <p class="video_title"></p>
</div>

最佳答案

试试这个

更改:

var fl_obj_template = $('<object width="260" height="140">' +    
                          '<param name="movie" value=""></param>' +   
                          '<param name="allowFullScreen" value="true"></param>' +   
                          '<param name="allowscriptaccess" value="always"></param>' +   
                          '<embed src="" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="260" height="140"></embed>' +   
                          '</object>'); 

对此:

var fl_obj_template = '<object width="260" height="140">' +    
                          '<param name="movie" value=""></param>' +   
                          '<param name="allowFullScreen" value="true"></param>' +   
                          '<param name="allowscriptaccess" value="always"></param>' +   
                          '<embed src="" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="260" height="140"></embed>' +   
                          '</object>';  

我认为 youtube 代码被用作选择器

关于jquery - 使用jquery动态嵌入youtube视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2969861/

相关文章:

Javascript 检索字符后的哈希值

javascript - 使用 jQuery 动态添加 onClick 事件

javascript - 仅在字符串中查找包含字母的单词

javascript - Mediaelement.js 无法使用 Flash-fallback 进行播放

javascript - 在 YouTube API 中检测事件源

javascript - 使用取消绑定(bind),我收到 Javascript TypeError : Object function has no method 'split'

flash - 是否可以通过Flash/js/silverlight在浏览器中捕获音频流?

c# - 使用YouTube数据API上传视频

javascript - 如果播放是由postMessage触发的,则不会触发Youtube的onStateChange

flash - 多次启动 Adob​​e AIR 应用程序