首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Google Images API

使用Google Images API
EN

Stack Overflow用户
提问于 2011-08-19 01:43:38
回答 2查看 4.4K关注 0票数 2

我正在尝试使用Google的Images API来搜索图像,并将其作为div放入我的html文档中。这就是我到目前为止所拥有的,但似乎什么都没有出现。这是http://code.google.com/apis/imagesearch/v1/devguide.html的零件。这是我第一次使用API,所以我不确定到底是怎么回事。

代码语言:javascript
复制
<html> 
  <head> 
    <title>Art Project FTW</title> 
  </head> 
  <body> 
  <br>
  <br>

    <form name="upload" method="post" action="parse_image.php" enctype="multipart/form-data"> 
      <input type="file" name="Image"><br>  
      <input type="submit" value="Upload Image"> 
    </form> 
    <script type="text/javascript" src="https://www.google.com/jsapi?key=xxx"></script>
    <script type="text/javascript" charset="utf-8">

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('content');
    contentDiv.innerHTML = '';

    // Loop through our results, printing them to the page.
    var results = searcher.results;
    for (var i = 0; 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 title = document.createElement('h2');
      // We use titleNoFormatting so that no HTML tags are left in the title
      title.innerHTML = result.titleNoFormatting;

      var newImg = document.createElement('img');
      // There is also a result.url property which has the escaped version
      newImg.src = result.tbUrl;

      imgContainer.appendChild(title);
      imgContainer.appendChild(newImg);

      // Put our title + image in the content
      contentDiv.appendChild(imgContainer);
    }
  }
}

function onload() {
  // Our ImageSearch instance.
  var imageSearch = new google.search.ImageSearch();

  // Restrict to extra large images only
  imageSearch.setRestriction(google.search.ImageSearch.RESTRICT_IMAGESIZE,
                             google.search.ImageSearch.IMAGESIZE_MEDIUM);

  // Here we set a callback so that anytime a search is executed, it will call
  // the searchComplete function and pass it our ImageSearch searcher.
  // When a search completes, our ImageSearch object is automatically
  // populated with the results.
  imageSearch.setSearchCompleteCallback(this, searchComplete, [imageSearch]);

  // Find me a beautiful car.
  imageSearch.execute("Subaru STI");
}
google.setonloadCallback(onload);​

</script>

  </body>
</html>

提前感谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-04-02 18:48:41

它无法工作,因为您正在查找ID =‘HTMLElement’的内容,您没有任何具有该ID的元素

票数 1
EN

Stack Overflow用户

发布于 2012-01-07 05:30:53

尝试将您的js函数放在<head></head>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7111742

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档