2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: 587d7fae367417b2b2512be5
|
2020-12-16 00:37:30 -07:00
|
|
|
|
title: 将 JSON 数据转换为 HTML
|
2018-10-10 18:03:03 -04:00
|
|
|
|
challengeType: 6
|
2020-09-07 16:18:38 +08:00
|
|
|
|
forumTopicId: 16807
|
2021-01-13 03:31:00 +01:00
|
|
|
|
dashedName: convert-json-data-to-html
|
2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --description--
|
|
|
|
|
|
2021-01-12 23:56:49 +08:00
|
|
|
|
现在你从 JSON API 获取了数据,可以在 HTML 中显示它们了。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
2021-01-12 23:56:49 +08:00
|
|
|
|
既然 cat photo 对象都保存在数组里,你可以使用 `forEach` 方法来遍历它们。当你拿到每个对象时,你就可以修改 HTML 元素了。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
2021-01-12 23:56:49 +08:00
|
|
|
|
首先,通过 `let html = "";` 声明一个 html 变量。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
2021-01-12 23:56:49 +08:00
|
|
|
|
接着,遍历 JSON,将用 `strong` 标签包裹的键名和后面跟着值的 html 元素添加给变量。当循环结束后渲染它。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
2020-09-07 16:18:38 +08:00
|
|
|
|
这是执行此操作的代码:
|
|
|
|
|
|
|
|
|
|
```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>";
|
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
2021-01-12 23:56:49 +08:00
|
|
|
|
**注意:** 在本挑战中,你需要给页面添加新的 HTML 元素,不能依赖 `textContent` 方法,这个方法容易遭受跨站脚本攻击,可以用 `innerHTML` 来完成挑战。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
|
|
# --instructions--
|
|
|
|
|
|
2021-01-12 23:56:49 +08:00
|
|
|
|
添加一个 `forEach` 循环来遍历 JSON 数据,并创建 HTML 元素以显示它。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-09-07 16:18:38 +08:00
|
|
|
|
以下是一个 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"
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --hints--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-01-12 23:56:49 +08:00
|
|
|
|
你的代码应该将数据保存在 `html` 变量中。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert(code.match(/html\s+?(\+=|=\shtml\s\+)/g));
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
|
|
|
|
|
2021-01-12 23:56:49 +08:00
|
|
|
|
你的代码应该使用 `forEach` 方法来遍历 API 中的 JSON 数据。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert(code.match(/json\.forEach/g));
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
|
|
|
|
|
2021-01-12 23:56:49 +08:00
|
|
|
|
你的代码应该用 `strong` 标签包裹键名。
|
2020-09-07 16:18:38 +08:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert(code.match(/<strong>.+<\/strong>/g));
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
2020-08-13 17:24:35 +02:00
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
|
# --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>
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --solutions--
|
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
|
```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>
|
|
|
|
|
```
|