通过附加的代码,我根据用户名在 youtube 上进行了搜索,并显示了结果。如果我搜索两次,结果就会相加。我想删除以前的结果。我尝试使用 htmlString = card; 但它只显示一个结果。感谢所有想帮助我解决这个问题的人。var musicCards = [];jQuery(document).ready(function() { jQuery("#searchButton").on("click", function() { var query = jQuery("#queryInput").val(); if (query != "") { loadYoutubeService(query); console.log(query + ""); } });});function loadYoutubeService(query) { gapi.client.load('youtube', 'v3', function() { gapi.client.setApiKey('ADADADADADA'); search(query); });}function search(query) { var request = gapi.client.youtube.search.list({ part: 'snippet', q: query, type: 'channel', maxResults: 15 }); request.execute(function(response) { jQuery.each(response.items, function(i, item) { if (!item['']) { var musicCard = {}; musicCard._id = item['snippet']['customUrl']; musicCard.title = item['snippet']['title']; musicCard.linkprofilo = item['snippet']['channelId']; musicCard.url = "https://www.youtube.com/channel/"; musicCard.description = item['snippet']['description']; musicCard.immagine = item['snippet']['thumbnails']['high']['url']; musicCards.push(musicCard); } }); renderView(); });}function renderView() { var htmlString = ""; musicCards.forEach(function(musicCard, i) { var card = createCard(musicCard._id, musicCard.title, musicCard.description, musicCard.url,musicCard.immagine, musicCard.linkprofilo); htmlString += card; }); jQuery('#youtube-utente').html(htmlString);}}<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
1 回答
汪汪一只猫
TA贡献1898条经验 获得超8个赞
解决使用:
request.execute(function(response) {
musicCards.length = 0; // clear array
添加回答
举报
0/150
提交
取消