使用谷歌图像API
Using Google Images API
我试图使用谷歌的图像API来搜索图像并将其放入我的html文档中作为div。这就是我到目前为止所拥有的,但似乎没有出现任何东西。这是来自http://code.google.com/apis/imagesearch/v1/devguide.html的零件。这是我第一次使用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>
提前感谢!
它不能工作,因为你正在寻找一个具有ID='content'的HTMLElement,你没有任何具有该ID的元素
试着把你的js函数放在<head></head>
相关文章:
- Steam Web API:I'我不知道该如何处理这个看起来像哈希的url来生成图像
- 图像未加载javascript、html、angular、imdb-api
- Imgur API图像搜索未返回数据
- Google Places API显示雷达搜索中的位置图标/图像
- API让'你在编辑/操纵图像吗
- 我如何从一个无限滚动的网站抓取图像,其中api是隐藏的,我无法通过使用Inspect Element获得它->网络
- 从imgur api的另一个浏览器拖放图像
- 如何检索从桌面应用程序发送到Rails API的图像
- 使用Facebook的javascript api发布多部分/表单数据编码的图像
- Instagram API 未加载所有图像
- Google Map JS API-加载地图分幅,但所有图像(标记、缩放控制等)都不加载;t负载2分钟
- 如何通过js将二进制代码的验证图像从api转换为png
- HTML5文件API使用Ajax上传多个图像
- 谷歌地球Api改变图像对比度
- 是否可以调用Google API获取地图图像并将图像保存在某个位置
- 如何使用Node.js显示从http请求到外部API的图像
- HTML5 文件阅读器 API - 图像质量
- ReCAPTCHA Ajax API图像定制
- 节点REST API图像渲染问题
- 如何在javascript中获取文件api图像大小