Dynamically get thumbnails and titles of vimeo videos

别来无恙 提交于 2020-01-01 05:32:08

问题


Picking up from this question: Get img thumbnails from Vimeo?

I'm trying to create a page with several vimeo videos on it.

I want to have the videos in the HTML look like this:

<div id='12345678' class='vimeo'></div>
<div id='23423423' class='vimeo'></div>

And then I want the jQuery to populate the divs with an img where the src is the thumbnail from vimeo, and an a which points to the video, and whose text is the title of the video. That is, it should end up like this:

<div id='12345678' class='vimeo'>
    <a href='https://vimeo.com/12345678'>
        <img src='url-to-thumbnail.jpg' />
        Video Title
    </a>
</div>

(Indentation added for clarity)

This is my starting point from that other question:

<script type="text/javascript">
    $.ajax({
        type:'GET',
        url: 'http://vimeo.com/api/v2/video/' + video_id + '.json',
        jsonp: 'callback',
        dataType: 'jsonp',
        success: function(data){
            var thumbnail_src = data[0].thumbnail_large;
            $('#thumb_wrapper').append('<img src="' + thumbnail_src + '"/>');
        }
    });
</script>

<div id="thumb_wrapper"></div>

回答1:


Try this:-

Assuming the div with id already exists.

  success: function(data){

              $('<a/>',{
                 href:data[0].url,
                 text:data[0].title }).append(
              $('<img/>' ,{
                 src:data[0].thumbnail_large
              })
              )
              .appendTo('#'+data[0].id)
        }

If the div with id doesnot exist:-

     success: function(data){

              $('<a/>',{
                 href:data[0].url,
                 text:data[0].title })
              .append(
                 $('<img/>' ,{
                 src:data[0].thumbnail_large
                 })
              ).appendTo($('<div/>',{
                 id:data[0].id,
                 class:'vimeo'

               }))
               .appendTo('.someContainer');
        }

Fiddle




回答2:


Append html to the corresponding div

var vimeo_content="<a>\
                        <img />\
                        <span></span>\
                    </a>";
// on success function
function(){
    var vdata = data[0];
    var thumbnail_src = vdata.thumbnail_large;
    var video_title   = vdata.title;
    var video_url     = vdata.url;

    $vimeo_content    = $(vimeo_content);
    $vimeo_content.find('a').attr('href',url);
    $vimeo_content.find('img').attr('src',thumbnail_src);
    $vimeo_content.find('span').text(title);
    //append to corresponding div
    $vimeo_content.appendTo($('div#'+video_id));
}


来源:https://stackoverflow.com/questions/16555155/dynamically-get-thumbnails-and-titles-of-vimeo-videos

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!