gpt4 book ai didi

javascript - 在页面中多次执行谷歌图像搜索

转载 作者:行者123 更新时间:2023-11-28 09:11:49 25 4
gpt4 key购买 nike

我想编写一个网页,它可以动态生成从谷歌搜索中获取的图像。这些图像的搜索词是不同的,所以我需要多次执行google搜索,而我发现这非常困难。我尝试根据google提供的源代码修改这些代码,但它只能执行一次搜索:

    <html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Google Search API Sample</title>
<script src="https://www.google.com/jsapi"></script>
<script type="text/javascript">

google.load('search', '1');

var imageSearch;
var keyword="sexy";

function searchComplete() {


// Check that we got results
if (imageSearch.results && imageSearch.results.length > 0) {

// Grab our content div, clear it.
var contentDiv = document.getElementById('content');
contentDiv.innerHTML = '';


var results = imageSearch.results;
for (var i = 0; i < results.length; i++) {
// For each result 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);
}

//clear search
imageSearch.clearResults();

}
}

function OnLoad() {

// Create an Image Search instance.
imageSearch = new google.search.ImageSearch();

// Set searchComplete as the callback function when a search is
// complete. The imageSearch object will have results in it.
imageSearch.setSearchCompleteCallback(this, searchComplete, null);

imageSearch.execute(keyword);
}


function hi(){
keyword="usa";
alert('hi');
google.setOnLoadCallback(OnLoad);
imageSearch.execute(keyword);
}


google.setOnLoadCallback(OnLoad);

</script>

</head>
<body style="font-family: Arial;border: 0 none;">
<button value="hi" onClick="hi">hi</button>
<div id="content">Loading...</div>
</body>
</html>

程序只能在OnLoad方法中执行搜索。实际上,我尝试将 google.setOnLoadCallback(OnLoad) 放入 hi() 函数中多次调用,但没有成功。希望有人能帮助我解决这些问题..

最佳答案

更改<button value="hi" onClick="hi">hi</button>

<button value="hi" onClick="hi()">hi</button>

关于javascript - 在页面中多次执行谷歌图像搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16218498/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com