简体   繁体   English

打破JavaScript循环

[英]Break javascript loop

I have following code to use google images search API: 我有以下代码来使用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);

The problem is, it gives me 3 image results. 问题是,它给了我3张图像结果。 How to break a loop and show only the 1st one instead of 3 images? 如何中断循环并仅显示第一个而不是3个图像? if I change for (var i = 1; i < results.length; i++) to for (var i = 3; i < results.length; i++) it shows only one image, but image shown is the 3rd one and I need to show 1st one :) Please advice 如果我将for (var i = 1; i < results.length; i++)更改for (var i = 3; i < results.length; i++)它仅显示一张图像,但是显示的图像是第三张图像,我需要显示第一个:)请指教

Don't use a for loop at all. 根本不要使用for循环。 Just replace all instances of i with 0. 只需将i所有实例替换为0。

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 = '';

        var result = searcher.results[0];

        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);

0 means the first item returned (almost all number sequences in programming start at 0!) so all other results will be ignored. 0表示返回的第一项(编程中几乎所有数字序列都从0开始!),因此所有其他结果将被忽略。

use break statement . 使用break statement It will terminate the loop once the image is found and hence you will have only the first one. 一旦找到图像,它将终止循环,因此您将只有第一个图像。

      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);
       //Berore the end of the loop
      if(i==1)
      {
      break;
      }
   }

When you only want one element, you don't need a for loop. 当您只需要一个元素时,就不需要for循环。 You can access the first element of an array with 您可以使用以下命令访问数组的第一个元素

result = results[0];

Arrays are zero-based. 数组从零开始。 So when it contains three images, the images are named results[0], results[1] and results[2]. 因此,当包含三个图像时,这些图像分别命名为results [0],results [1]和results [2]。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM