200 lines
4.4 KiB
Markdown
200 lines
4.4 KiB
Markdown
![]() |
---
|
|||
|
id: 587d7fae367417b2b2512be5
|
|||
|
title: 將 JSON 數據轉換爲 HTML
|
|||
|
challengeType: 6
|
|||
|
forumTopicId: 16807
|
|||
|
dashedName: convert-json-data-to-html
|
|||
|
---
|
|||
|
|
|||
|
# --description--
|
|||
|
|
|||
|
現在你從 JSON API 獲取了數據,可以在 HTML 中顯示它們了。
|
|||
|
|
|||
|
既然 cat photo 對象都保存在數組裏,你可以使用 `forEach` 方法來遍歷它們。 當你拿到每個對象時,你就可以修改 HTML 元素了。
|
|||
|
|
|||
|
首先,通過 `let html = "";` 聲明一個 html 變量。
|
|||
|
|
|||
|
接着,遍歷 JSON,將用 `strong` 標籤包裹的鍵名和後面跟着值的 html 元素添加給變量。 當循環結束後渲染它。
|
|||
|
|
|||
|
這是執行此操作的代碼:
|
|||
|
|
|||
|
```js
|
|||
|
let html = "";
|
|||
|
json.forEach(function(val) {
|
|||
|
const keys = Object.keys(val);
|
|||
|
html += "<div class = 'cat'>";
|
|||
|
keys.forEach(function(key) {
|
|||
|
html += "<strong>" + key + "</strong>: " + val[key] + "<br>";
|
|||
|
});
|
|||
|
html += "</div><br>";
|
|||
|
});
|
|||
|
```
|
|||
|
|
|||
|
**注意:**在本挑戰中,你需要給頁面添加新的 HTML 元素,所以你不能使用 `textContent` 方法。 你可以用 `innerHTML` 來完成挑戰,這個方法使網站容易遭受跨站腳本攻擊。
|
|||
|
|
|||
|
# --instructions--
|
|||
|
|
|||
|
添加一個 `forEach` 循環來遍歷 JSON 數據,並創建 HTML 元素以顯示它。
|
|||
|
|
|||
|
下面是示例 JSON:
|
|||
|
|
|||
|
```json
|
|||
|
[
|
|||
|
{
|
|||
|
"id":0,
|
|||
|
"imageLink":"https://s3.amazonaws.com/freecodecamp/funny-cat.jpg",
|
|||
|
"altText":"A white cat wearing a green helmet shaped melon on its head. ",
|
|||
|
"codeNames":[ "Juggernaut", "Mrs. Wallace", "Buttercup"
|
|||
|
]
|
|||
|
}
|
|||
|
]
|
|||
|
```
|
|||
|
|
|||
|
# --hints--
|
|||
|
|
|||
|
應該將數據保存在 `html` 變量中。
|
|||
|
|
|||
|
```js
|
|||
|
assert(__helpers.removeWhiteSpace(code).match(/html(\+=|=html\+)/g))
|
|||
|
```
|
|||
|
|
|||
|
應該使用 `forEach` 方法來遍歷 API 中的 JSON 數據。
|
|||
|
|
|||
|
```js
|
|||
|
assert(code.match(/json\.forEach/g));
|
|||
|
```
|
|||
|
|
|||
|
應該用 `strong` 標籤包裹鍵名。
|
|||
|
|
|||
|
```js
|
|||
|
assert(code.match(/<strong>.+<\/strong>/g));
|
|||
|
```
|
|||
|
|
|||
|
# --seed--
|
|||
|
|
|||
|
## --seed-contents--
|
|||
|
|
|||
|
```html
|
|||
|
<script>
|
|||
|
document.addEventListener('DOMContentLoaded', function(){
|
|||
|
document.getElementById('getMessage').onclick = function(){
|
|||
|
const req = new XMLHttpRequest();
|
|||
|
req.open("GET",'/json/cats.json',true);
|
|||
|
req.send();
|
|||
|
req.onload = function(){
|
|||
|
const json = JSON.parse(req.responseText);
|
|||
|
let html = "";
|
|||
|
// Add your code below this line
|
|||
|
|
|||
|
|
|||
|
// Add your code above this line
|
|||
|
document.getElementsByClassName('message')[0].innerHTML = html;
|
|||
|
};
|
|||
|
};
|
|||
|
});
|
|||
|
</script>
|
|||
|
|
|||
|
<style>
|
|||
|
body {
|
|||
|
text-align: center;
|
|||
|
font-family: "Helvetica", sans-serif;
|
|||
|
}
|
|||
|
h1 {
|
|||
|
font-size: 2em;
|
|||
|
font-weight: bold;
|
|||
|
}
|
|||
|
.box {
|
|||
|
border-radius: 5px;
|
|||
|
background-color: #eee;
|
|||
|
padding: 20px 5px;
|
|||
|
}
|
|||
|
button {
|
|||
|
color: white;
|
|||
|
background-color: #4791d0;
|
|||
|
border-radius: 5px;
|
|||
|
border: 1px solid #4791d0;
|
|||
|
padding: 5px 10px 8px 10px;
|
|||
|
}
|
|||
|
button:hover {
|
|||
|
background-color: #0F5897;
|
|||
|
border: 1px solid #0F5897;
|
|||
|
}
|
|||
|
</style>
|
|||
|
|
|||
|
<h1>Cat Photo Finder</h1>
|
|||
|
<p class="message box">
|
|||
|
The message will go here
|
|||
|
</p>
|
|||
|
<p>
|
|||
|
<button id="getMessage">
|
|||
|
Get Message
|
|||
|
</button>
|
|||
|
</p>
|
|||
|
```
|
|||
|
|
|||
|
# --solutions--
|
|||
|
|
|||
|
```html
|
|||
|
<script>
|
|||
|
document.addEventListener('DOMContentLoaded', function(){
|
|||
|
document.getElementById('getMessage').onclick = function(){
|
|||
|
const req = new XMLHttpRequest();
|
|||
|
req.open("GET",'/json/cats.json',true);
|
|||
|
req.send();
|
|||
|
req.onload = function(){
|
|||
|
const json = JSON.parse(req.responseText);
|
|||
|
let html = "";
|
|||
|
// Add your code below this line
|
|||
|
json.forEach(function(val) {
|
|||
|
var keys = Object.keys(val);
|
|||
|
html += "<div class = 'cat'>";
|
|||
|
keys.forEach(function(key) {
|
|||
|
html += "<strong>" + key + "</strong>: " + val[key] + "<br>";
|
|||
|
});
|
|||
|
html += "</div><br>";
|
|||
|
});
|
|||
|
// Add your code above this line
|
|||
|
document.getElementsByClassName('message')[0].innerHTML = html;
|
|||
|
};
|
|||
|
};
|
|||
|
});
|
|||
|
</script>
|
|||
|
|
|||
|
<style>
|
|||
|
body {
|
|||
|
text-align: center;
|
|||
|
font-family: "Helvetica", sans-serif;
|
|||
|
}
|
|||
|
h1 {
|
|||
|
font-size: 2em;
|
|||
|
font-weight: bold;
|
|||
|
}
|
|||
|
.box {
|
|||
|
border-radius: 5px;
|
|||
|
background-color: #eee;
|
|||
|
padding: 20px 5px;
|
|||
|
}
|
|||
|
button {
|
|||
|
color: white;
|
|||
|
background-color: #4791d0;
|
|||
|
border-radius: 5px;
|
|||
|
border: 1px solid #4791d0;
|
|||
|
padding: 5px 10px 8px 10px;
|
|||
|
}
|
|||
|
button:hover {
|
|||
|
background-color: #0F5897;
|
|||
|
border: 1px solid #0F5897;
|
|||
|
}
|
|||
|
</style>
|
|||
|
|
|||
|
<h1>Cat Photo Finder</h1>
|
|||
|
<p class="message">
|
|||
|
The message will go here
|
|||
|
</p>
|
|||
|
<p>
|
|||
|
<button id="getMessage">
|
|||
|
Get Message
|
|||
|
</button>
|
|||
|
</p>
|
|||
|
```
|