需要帮助使用 jQuery (YouTube API v3) 从视频中获取标题
Need help getting the title from a video using jQuery (YouTube API v3)
所以,我最近偶然发现了如何使用 YouTube API v3,并且我个人从他们的 API 中学到了很多东西 - 我想要的我知道如何获取视频的标题,以便在搜索术语时显示视频的标题。
这是我的代码:http://codepen.io/mistkaes/pen/MYqrXW
一如既往,感谢您的帮助!
function openIt(videoId) {
ideVideo = videoId,
container = "#abrePlayer";
//open a lightbox, or do something else with the video id. In this case, let's create an iframe to play the little bastard.
iframe = '<iframe width="600" height="400" src="//www.youtube.com/embed/'+ideVideo+'" frameborder="0" allowfullscreen></iframe>';
$(container).html(iframe)
};
$(document).ready(function() {
//find using typed word
$('#searchIt').keyup(function() {
var itemsPerPage = 20;
var term = $(this).val();
var searchUrl = 'http://gdata.youtube.com/feeds/api/videos?q='+term+'&format=5&max-results='+itemsPerPage+'&v=2&alt=jsonc';
$.ajax({
type: "GET",
url: searchUrl,
dataType: "jsonp",
success: function(res) {
$("#resultadosGerais").html('');
console.log();
if(res.data.items){
//if there are items in our response
var item = res.data.items;
$.each(item, function(i, data) {
var videoId = data.id;
var thumb = "https://i.ytimg.com/vi/" + videoId + "/default.jpg";
var showThis = '<div id=\"result\" onClick=\"openIt(\''+ videoId + '\');"> <img src=\"' + thumb + '\" /><p id=\"media-header\">'+videoId+'</p></div><br />';
$("#resultadosGerais").append(showThis);
});
} else {
$("#resultadosGerais").append('No Results Found');
}
}})
});
});
您可以使用示例 URL,即 http://gdata.youtube.com/feeds/api/videos?q=Beethoven&format=5&max-results=1&v=2&alt=jsonc
,轻松分析您从 YouTube API 获得的响应
这个 returns 一个 JSON 数组,您正在预览中显示它。我相信您是想在那里显示标题而不是 ID?
var showThis = '<div id=\"result\" onClick=\"openIt(\''+ videoId + '\');"> <img src=\"' + thumb + '\" /><p id=\"media-header\">'+data.title+'</p></div><br />';
$("#resultadosGerais").append(showThis);
这里的关键是data.title
,其中包含视频的标题
所以,我最近偶然发现了如何使用 YouTube API v3,并且我个人从他们的 API 中学到了很多东西 - 我想要的我知道如何获取视频的标题,以便在搜索术语时显示视频的标题。
这是我的代码:http://codepen.io/mistkaes/pen/MYqrXW
一如既往,感谢您的帮助!
function openIt(videoId) {
ideVideo = videoId,
container = "#abrePlayer";
//open a lightbox, or do something else with the video id. In this case, let's create an iframe to play the little bastard.
iframe = '<iframe width="600" height="400" src="//www.youtube.com/embed/'+ideVideo+'" frameborder="0" allowfullscreen></iframe>';
$(container).html(iframe)
};
$(document).ready(function() {
//find using typed word
$('#searchIt').keyup(function() {
var itemsPerPage = 20;
var term = $(this).val();
var searchUrl = 'http://gdata.youtube.com/feeds/api/videos?q='+term+'&format=5&max-results='+itemsPerPage+'&v=2&alt=jsonc';
$.ajax({
type: "GET",
url: searchUrl,
dataType: "jsonp",
success: function(res) {
$("#resultadosGerais").html('');
console.log();
if(res.data.items){
//if there are items in our response
var item = res.data.items;
$.each(item, function(i, data) {
var videoId = data.id;
var thumb = "https://i.ytimg.com/vi/" + videoId + "/default.jpg";
var showThis = '<div id=\"result\" onClick=\"openIt(\''+ videoId + '\');"> <img src=\"' + thumb + '\" /><p id=\"media-header\">'+videoId+'</p></div><br />';
$("#resultadosGerais").append(showThis);
});
} else {
$("#resultadosGerais").append('No Results Found');
}
}})
});
});
您可以使用示例 URL,即 http://gdata.youtube.com/feeds/api/videos?q=Beethoven&format=5&max-results=1&v=2&alt=jsonc
,轻松分析您从 YouTube API 获得的响应这个 returns 一个 JSON 数组,您正在预览中显示它。我相信您是想在那里显示标题而不是 ID?
var showThis = '<div id=\"result\" onClick=\"openIt(\''+ videoId + '\');"> <img src=\"' + thumb + '\" /><p id=\"media-header\">'+data.title+'</p></div><br />';
$("#resultadosGerais").append(showThis);
这里的关键是data.title
,其中包含视频的标题