我有一个基于youtube apiv3在youtube上投影视频的代码 我的问题是如何将每个li的id自动递增1个单位,因为它会附加到结果中 我尝试了几种使用PHP循环结果的方法,但我不断得到不需要的结果。
<script>
$.get(
"https://www.googleapis.com/youtube/v3/playlistItems", data,
function(data){
var output;
$('#results').html("");
$.each(data.items, function(i, item){
console.log(item);
videoTitle = item.snippet.title;
rvideoID = item.snippet.resourceId.videoId;
vidThumburl = item.snippet.thumbnails.high.url;
<?php $counter = 1; while($counter <= 10 ){ ?>
output = '<li id="menu<?=$counter;?>" style="list-style:none;"><a class="videos2 video" href="https://www.youtube.com/watch?v=' + rvideoID + '">' + videoTitle + '<a/></li>';
//Append to results list
$('#results').append(output);
<?php $counter++; } ?>
});
try{
if(data.prevPageToken){
$("#results").append('<li id="menu<?=$counter;?>" style="list-style:none;"><a class="" href="javascript:void(0);" onclick="javascript:getVids(pid, \'' + data.prevPageToken + '\');">» Prev Page<a/></li>');
}
}catch(err){
}
try{
if(data.nextPageToken){
$("#results").append('<li id="menu<?=$counter;?> style="list-style:none;"><a class="" href="javascript:void(0);" onclick="javascript:getVids(pid, \'' + data.nextPageToken + '\');">Next Page «<a/></li>');
}
}catch(err){
}
});
}
</script>
<body>
<div id="container">
<ul id="results"></ul>
</div>
</body>
答案 0 :(得分:0)
只需将迭代器放入javascript代码
即可var counter = 1;
$.each(data.items, function(i, item){
console.log(item);
videoTitle = item.snippet.title;
rvideoID = item.snippet.resourceId.videoId;
vidThumburl = item.snippet.thumbnails.high.url;
output = '<li id="menu' + counter + '" style="list-style:none;">' +
'<a class="videos2 video" href="https://www.youtube.com/watch?v=' + rvideoID + '">' + videoTitle + '<a/>'+
'</li>';
//Append to results list
$('#results').append(output);
counter++;
});