粘贴网址后,如何像在Facebook墙上的表格一样,在表格下方显示youtube视频数据?像下面这样:
如何才能做到这一点?有没有jQuery插件能够做到这一点?
对于图像网址,我也将感激不尽。
谢谢。
最佳答案
这个jsfiddle-使用YouTube API-将为您提供一个入门的基本示例:
http://jsfiddle.net/g3x84/1/
这是完整的示例(因为我还必须包括一些代码)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
</style>
<script type="text/javascript">
function parse_str (str, array)
{
var strArr = String(str).replace(/^&/, '').replace(/&$/, '').split('&'),
sal = strArr.length,
fixStr = function (str)
{
return decodeURIComponent(str.replace(/\+/g, '%20'));
},
i, j, ct, p, lastObj, obj, lastIter, undef, chr, tmp, key, value, postLeftBracketPos, keys, keysLen;
if (!array)
array = this.window;
for (i = 0; i < sal; i++)
{
tmp = strArr[i].split('=');
key = fixStr(tmp[0]);
value = (tmp.length < 2) ? '' : fixStr(tmp[1]);
while (key.charAt(0) === ' ')
key = key.slice(1);
if (key.indexOf('\x00') > -1)
key = key.slice(0, key.indexOf('\x00'));
if (key && key.charAt(0) !== '[')
{
keys = [];
postLeftBracketPos = 0;
for (j = 0; j < key.length; j++)
{
if (key.charAt(j) === '[' && !postLeftBracketPos)
{
postLeftBracketPos = j + 1;
}
else if (key.charAt(j) === ']')
{
if (postLeftBracketPos)
{
if (!keys.length)
keys.push(key.slice(0, postLeftBracketPos - 1));
keys.push(key.substr(postLeftBracketPos, j - postLeftBracketPos));
postLeftBracketPos = 0;
if (key.charAt(j + 1) !== '[')
break;
}
}
}
if (!keys.length)
keys = [key];
for (j = 0; j < keys[0].length; j++)
{
chr = keys[0].charAt(j);
if (chr === ' ' || chr === '.' || chr === '[')
keys[0] = keys[0].substr(0, j) + '_' + keys[0].substr(j + 1);
if (chr === '[')
break;
}
obj = array;
for (j = 0, keysLen = keys.length; j < keysLen; j++)
{
key = keys[j].replace(/^['"]/, '').replace(/['"]$/, '');
lastIter = j !== keys.length - 1;
lastObj = obj;
if ((key !== '' && key !== ' ') || j === 0)
{
if (obj[key] === undef)
obj[key] = {};
obj = obj[key];
}
else
{
ct = -1;
for (p in obj)
{
if (obj.hasOwnProperty(p))
{
if (+p > ct && p.match(/^\d+$/g))
ct = +p;
}
}
key = ct + 1;
}
}
lastObj[key] = value;
}
}
}
function search(url)
{
if (url.indexOf("?") === -1 || url.indexOf("v=") === -1)
return;
var query_string = url.substr(url.indexOf("?")+1),
arr = [];
parse_str(query_string, arr);
var xhr = new XMLHttpRequest();
xhr.onload = function(oEvent)
{
if (xhr.status == 200 && xhr.responseText)
{
var obj = JSON.parse(xhr.responseText);
var content = [];
content.push(
'<img src="//img.youtube.com/vi/' + arr['v'] + '/0.jpg" style="width:80px; height:80px;" />',
obj.entry.title['$t'],
url,
obj.entry['media$group']['media$description']['$t'],
obj.entry.author[0].name['$t']
);
document.getElementById('data').innerHTML = content.join('<br /><br />');
}
}
xhr.open("GET", "https://gdata.youtube.com/feeds/api/videos/" + arr['v'] + "?v=2&alt=json", false)
xhr.send(null);
}
</script>
</head>
<body>
Input YouTube video URL:<br />
<input type="text" id="url" oninput="search(this.value)" /><br />
<br />
<div class="data" id="data">
</div>
</body>
</html>
注意-parse_str函数由php.js提供
关于javascript - 粘贴网址后如何显示youtube视频数据,就像Facebook的Wall表单一样?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15841407/