当我在stackoverflow上搜索时,我发现以下代码:
$(document).ready(function () {
$("#show").click(function () {
getYoutube($("#Search").val());
});
});
function getYoutube(title) {
$.ajax({
type: "GET",
url: yt_url = 'http://gdata.youtube.com/feeds/api/videos?q=' + title + '&format=5&max-results=1&v=2&alt=jsonc',
dataType: "jsonp",
success: function (response) {
if (response.data.items) {
$.each(response.data.items, function (i, data) {
var video_id = data.id;
var video_title = data.title;
var video_viewCount = data.viewCount;
$("#result").html(video_id);
});
} else {
$("#result").html('false');
}
}
});
}
如何编辑代码以仅保留功能?
我希望能够像这样使用它:getYoutube(my_keywords);
另外,如何将函数输出保存到变量?就像是 :
var_name = getYoutube(my_keywords);
会好吗?
谢谢! ;)
最佳答案
是的,您可以不使用$(document).ready
来使用它。不,函数不返回任何东西
要从Ajax“返回”值:
$.ajax({
...
success: function(dataFromServer) {
processServerOutput(dataFromServer);
}
});
function processServerOutput(someString) {
alert(someString);
}
关于javascript - Youtube Search js函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25376636/