-
Notifications
You must be signed in to change notification settings - Fork 0
/
mountain.html
26 lines (24 loc) · 1.03 KB
/
mountain.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>山</title>
</head>
<body>
<h1 style="font-size: 36px;">山</h1>
<img id="mountainImage" alt="山的图片">
<script>
// 使用 fetch 函数从 Google Custom Search API 获取图片链接
fetch("https://www.googleapis.com/customsearch/v1?key=AIzaSyAjKzvryZqVDSQVN_3yXPZSsDD9p84WDAY&cx=e101ca26e95984b4a&q=mountain&searchType=image&num=1")
.then(response => response.json()) // 将响应解析为 JSON
.then(data => {
// 从响应中获取图片链接
const imageUrl = data.items[0].link;
// 设置图片的 src 属性为获取到的图片链接
document.getElementById("mountainImage").src = imageUrl;
})
.catch(error => console.error("Error fetching image:", error)); // 捕获并处理任何错误
</script>
</body>
</html>