Google画像検索APIを使用する次のコードがあります:
google.load('search', '1');
function searchComplete(searcher) {
// Check that we got results
if (searcher.results && searcher.results.length > 0) {
// Grab our content div, clear it.
var contentDiv = document.getElementById('contentimg');
contentDiv.innerHTML = '';
// Loop through our results, printing them to the page.
var results = searcher.results;
for (var i = 1; i < results.length; i++) {
// For each result write it's title and image to the screen
var result = results[i];
var imgContainer = document.createElement('div');
var newImg = document.createElement('img');
// There is also a result.url property which has the escaped version
newImg.src = result.tbUrl;
imgContainer.appendChild(newImg);
// Put our title + image in the content
contentDiv.appendChild(imgContainer);
問題は、3 つの画像結果が得られることです。ループを中断して、3 つの画像ではなく最初の画像のみを表示するにはどうすればよいですか? 変更for (var i = 1; i < results.length; i++)
するfor (var i = 3; i < results.length; i++)
と画像が 1 つだけ表示されますが、表示される画像は 3 番目の画像であり、1 番目の画像を表示する必要があります :) アドバイスをお願いします