下面的代码是静态的我想通过搜索框
使其动态化<script type="text/javascript">
function youtubeFeedCallback(data) {
var s = '';
s += '<img src="' + data.entry.media$group.media$thumbnail[0].url + '" width="' + data.entry.media$group.media$thumbnail[0].width + '" height="' + data.entry.media$group.media$thumbnail[0].height + '" alt="' + data.entry.media$group.media$thumbnail[0].yt$name + '" align="right"/>';
s += '<b>Title:</b> ' + data.entry.title.$t + '<br/>';
s += '<b>Author:</b> ' + data.entry.author[0].name.$t + '<br/>';
s += '<b>Published:</b> ' + new Date(data.entry.published.$t).toLocaleDateString() + '<br/>';
s += '<b>Duration:</b> ' + Math.floor(data.entry.media$group.yt$duration.seconds / 60) + ':' + (data.entry.media$group.yt$duration.seconds % 60) + ' (' + data.entry.media$group.yt$duration.seconds + ' seconds)<br/>';
if (data.entry.gd$rating) {
s += '<b>Rating:</b> ' + data.entry.gd$rating.average.toFixed(1) + ' out of ' + data.entry.gd$rating.max + ' (' + data.entry.gd$rating.numRaters + ' ratings)<br/>';
}
s += '<b>Statistics:</b> ' + data.entry.yt$statistics.favoriteCount + ' favorite(s); ' + data.entry.yt$statistics.viewCount + ' view(s)<br/>';
s += '<br/>' + data.entry.media$group.media$description.$t.replace(/\n/g, '<br/>') + '<br/>';
s += '<br/><a href="' + data.entry.media$group.media$player.url + '" target="_blank">Watch on YouTube</a>';
document.write(s);
}
</script>
<script type="text/javascript" src="http://gdata.youtube.com/feeds/api/videos/gzDS-Kfd5XQ?v=2&alt=json-in-script&callback=youtubeFeedCallback"></script>
由于
答案 0 :(得分:0)
嗨请将此代码用于动态。
<script type="text/javascript" src="jquery-1.6.2.min.js"></script>
<script type="text/javascript">
function youtubeFeedCallback(data) {
var s = '';
s += '<img src="' + data.entry.media$group.media$thumbnail[0].url + '" width="' + data.entry.media$group.media$thumbnail[0].width + '" height="' + data.entry.media$group.media$thumbnail[0].height + '" alt="' + data.entry.media$group.media$thumbnail[0].yt$name + '" align="right"/>';
s += '<b>Title:</b> ' + data.entry.title.$t + '<br/>';
s += '<b>Author:</b> ' + data.entry.author[0].name.$t + '<br/>';
s += '<b>Published:</b> ' + new Date(data.entry.published.$t).toLocaleDateString() + '<br/>';
s += '<b>Duration:</b> ' + Math.floor(data.entry.media$group.yt$duration.seconds / 60) + ':' + (data.entry.media$group.yt$duration.seconds % 60) + ' (' + data.entry.media$group.yt$duration.seconds + ' seconds)<br/>';
if (data.entry.gd$rating) {
s += '<b>Rating:</b> ' + data.entry.gd$rating.average.toFixed(1) + ' out of ' + data.entry.gd$rating.max + ' (' + data.entry.gd$rating.numRaters + ' ratings)<br/>';
}
s += '<b>Statistics:</b> ' + data.entry.yt$statistics.favoriteCount + ' favorite(s); ' + data.entry.yt$statistics.viewCount + ' view(s)<br/>';
s += '<br/>' + data.entry.media$group.media$description.$t.replace(/\n/g, '<br/>') + '<br/>';
s += '<br/><a href="' + data.entry.media$group.media$player.url + '" target="_blank">Watch on YouTube</a>';
document.write(s);
}
function getVal(){
//slit url by id
var url = $('#name').val();
console.log(url);
var index = url.split('=');
$.ajax({
url : 'http://gdata.youtube.com/feeds/api/videos/' + index[1] +'?v=2&alt=json-in-script&callback=youtubeFeedCallback',
dataType : "json",
type : "post",
method:'get',
success : function(data){
youtubeFeedCallback(data);
}
})}
</script>
<script type="text/javascript" src="http://gdata.youtube.com/feeds/api/videos/gHGDN9-oFJE?v=2&alt=json-in-script&callback=youtubeFeedCallback">
</script>
<input type='text' id='name' name='name'/>
<input type='button' name='btn' id='btn' onclick='getVal()' value='Search'/>
...