2011-08-18 94 views
2

我试图使用谷歌的图像API搜索图像,并把它作为一个div我的html文档。这是我迄今为止所做的,但似乎没有出现。这是来自http://code.google.com/apis/imagesearch/v1/devguide.html的零件。这是我第一次使用API​​,所以我不确定发生了什么。使用谷歌图像API

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

在此先感谢!

回答

1

你的JS功能,因为你正在寻找具有ID的HTML元素=“内容”,不能正常工作,你haven't任何元素与ID