Add languages Russian, Arabic, Chinese, Portuguese (#18305)
This commit is contained in:
committed by
mrugesh mohapatra
parent
09d3eca712
commit
2ca3a2093f
@ -0,0 +1,100 @@
|
||||
---
|
||||
id: 587d7fae367417b2b2512be4
|
||||
title: Access the JSON Data from an API
|
||||
challengeType: 6
|
||||
videoUrl: ''
|
||||
localeTitle: 从API访问JSON数据
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">在之前的挑战中,您了解了如何从freeCodeCamp Cat Photo API获取JSON数据。现在,您将仔细查看返回的数据,以便更好地理解JSON格式。回想一下JavaScript中的一些表示法: <blockquote> [] - >方括号代表一个数组<br> {} - >圆括号代表一个对象<br> “” - >双引号代表一个字符串。它们还用于JSON中的键名</blockquote>了解API返回的数据结构非常重要,因为它会影响您检索所需值的方式。在右侧,单击“获取消息”按钮,将freeCodeCamp Cat Photo API JSON加载到HTML中。您在JSON数据中看到的第一个和最后一个字符是方括号<code>[ ]</code> 。这意味着返回的数据是一个数组。 JSON数据中的第二个字符是一个卷曲<code>{</code>括号,它启动一个对象。仔细观察,您可以看到有三个独立的对象。 JSON数据是一个包含三个对象的数组,其中每个对象都包含有关猫照片的信息。您之前了解到,对象包含以逗号分隔的“键值对”。在Cat Photo示例中,第一个对象具有<code>"id":0</code>其中“id”是键,0是其对应值。类似地,有“imageLink”,“altText”和“codeNames”的键。每个猫照片对象具有相同的键,但具有不同的值。第一个对象中另一个有趣的“键值对”是<code>"codeNames":["Juggernaut","Mrs. Wallace","ButterCup"]</code> 。这里“codeNames”是键,它的值是三个字符串的数组。可以有对象数组以及带有数组作为值的键。记住如何访问数组和对象中的数据。数组使用括号表示法来访问项目的特定索引。对象使用括号或点表示法来访问给定属性的值。这是打印第一张猫照片的“altText”的示例 - 请注意编辑器中解析的JSON数据保存在名为<code>json</code>的变量中: <blockquote>的console.log(JSON [0] .altText); <br> //打印“头上戴着绿色头盔形瓜的白猫。” </blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">对于“id”为2的cat,在<code>codeNames</code>数组中向控制台输出第二个值。您应该在对象上使用括号和点表示法(保存在变量<code>json</code> )来访问该值。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 您的代码应使用括号和点符号来访问正确的代码名称,并将“Loki”打印到控制台。
|
||||
testString: 'assert(code.match(/(?:json\[2\]\.codeNames\[1\]|json\[2\]\[("|")codeNames\1\]\[1\])/g), "Your code should use bracket and dot notation to access the proper code name, and print "Loki" to the console.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='html-seed'>
|
||||
|
||||
```html
|
||||
<script>
|
||||
document.addEventListener('DOMContentLoaded',function(){
|
||||
document.getElementById('getMessage').onclick=function(){
|
||||
req=new XMLHttpRequest();
|
||||
req.open("GET",'/json/cats.json',true);
|
||||
req.send();
|
||||
req.onload=function(){
|
||||
json=JSON.parse(req.responseText);
|
||||
document.getElementsByClassName('message')[0].innerHTML=JSON.stringify(json);
|
||||
// Add your code below this line
|
||||
|
||||
|
||||
// Add your code above this line
|
||||
};
|
||||
};
|
||||
});
|
||||
</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>
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,93 @@
|
||||
---
|
||||
id: 587d7fad367417b2b2512be2
|
||||
title: Change Text with click Events
|
||||
challengeType: 6
|
||||
videoUrl: ''
|
||||
localeTitle: 单击“事件”更改文本
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">当click事件发生时,您可以使用JavaScript来更新HTML元素。例如,当用户单击“获取消息”按钮时,它会使用类<code>message</code>更改元素的文本,使其<code>message</code> “此消息是”。这可以通过在click事件中添加以下代码来实现: <code>document.getElementsByClassName('message')[0].textContent="Here is the message";</code> </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">在<code>onclick</code>事件处理程序中添加代码,以更改<code>message</code>元素内的文本,说“这是消息”。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 您的代码应使用<code>document.getElementsByClassName</code>方法选择具有类<code>message</code>的元素,并将其<code>textContent</code>设置为给定的字符串。
|
||||
testString: 'assert(code.match(/document\.getElementsByClassName\(\s*?("|")message\1\s*?\)\[0\]\.textContent\s*?=\s*?("|")Here is the message\2/g), "Your code should use the <code>document.getElementsByClassName</code> method to select the element with class <code>message</code> and set its <code>textContent</code> to the given string.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='html-seed'>
|
||||
|
||||
```html
|
||||
<script>
|
||||
document.addEventListener('DOMContentLoaded',function(){
|
||||
document.getElementById('getMessage').onclick=function(){
|
||||
// Add your code below this line
|
||||
|
||||
|
||||
// Add your code above this line
|
||||
}
|
||||
});
|
||||
</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>
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,106 @@
|
||||
---
|
||||
id: 587d7fae367417b2b2512be5
|
||||
title: Convert JSON Data to HTML
|
||||
challengeType: 6
|
||||
videoUrl: ''
|
||||
localeTitle: 将JSON数据转换为HTML
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">现在您正在从JSON API获取数据,您可以在HTML中显示它。您可以使用<code>forEach</code>方法循环数据,因为cat照片对象保存在数组中。当您到达每个项目时,您可以修改HTML元素。首先,使用<code>var html = "";</code>声明一个html变量<code>var html = "";</code> 。然后,遍历JSON,将HTML添加到包含<code>strong</code>标记中的键名的变量,然后是值。循环结束后,渲染它。这是执行此操作的代码: <blockquote> json.forEach(function(val){ <br> var keys = Object.keys(val); <br> html + =“<div class ='cat'>”; <br> keys.forEach(function(key){ <br> html + =“<strong>”+ key +“</ strong>:”+ val [key] +“<br>”; <br> }); <br> html + =“</ div> <br>”; <br> }); </blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">添加<code>forEach</code>方法以循环JSON数据并创建HTML元素以显示它。这是一些JSON示例<blockquote> [ <br> { <br> “ID”:0, <br> “IMAGELINK”: “https://s3.amazonaws.com/freecodecamp/funny-cat.jpg” <br> “altText”:“头上戴着绿色头盔形状瓜的白猫。”, <br> “codeNames”:[“Juggernaut”,“华莱士夫人”,“毛茛” <br> ] <br> } <br> ] </blockquote></section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 您的代码应该将数据存储在<code>html</code>变量中
|
||||
testString: 'assert(code.match(/html\s+?(\+=|=\shtml\s\+)/g), "Your code should store the data in the <code>html</code> variable");'
|
||||
- text: 您的代码应该使用<code>forEach</code>方法来循环API中的JSON数据。
|
||||
testString: 'assert(code.match(/json\.forEach/g), "Your code should use a <code>forEach</code> method to loop over the JSON data from the API.");'
|
||||
- text: 您的代码应将密钥名称包装在<code>strong</code>标记中。
|
||||
testString: 'assert(code.match(/<strong>.+<\/strong>/g), "Your code should wrap the key names in <code>strong</code> tags.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='html-seed'>
|
||||
|
||||
```html
|
||||
<script>
|
||||
document.addEventListener('DOMContentLoaded',function(){
|
||||
document.getElementById('getMessage').onclick=function(){
|
||||
req=new XMLHttpRequest();
|
||||
req.open("GET",'/json/cats.json',true);
|
||||
req.send();
|
||||
req.onload=function(){
|
||||
json=JSON.parse(req.responseText);
|
||||
var 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>
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,64 @@
|
||||
---
|
||||
id: 587d7faf367417b2b2512be8
|
||||
title: Get Geolocation Data to Find A User's GPS Coordinates
|
||||
challengeType: 6
|
||||
videoUrl: ''
|
||||
localeTitle: 获取地理位置数据以查找用户的GPS坐标
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">您可以做的另一件很酷的事情是访问您用户的当前位置。每个浏览器都有一个内置的导航器,可以为您提供此信息。导航器将获得用户当前的经度和纬度。您将看到允许或阻止此站点了解您当前位置的提示。只要代码正确,挑战就可以以任何一种方式完成。通过选择允许,您将看到输出手机上的文本更改为您的纬度和经度。这是执行此操作的代码: <blockquote> if(navigator.geolocation){ <br> navigator.geolocation.getCurrentPosition(function(position){ <br> document.getElementById('data')。innerHTML =“latitude:”+ position.coords.latitude +“<br>经度:”+ position.coords.longitude; <br> }); <br> } </blockquote>首先,它检查<code>navigator.geolocation</code>对象是否存在。如果是,则调用该对象上的<code>getCurrentPosition</code>方法,该方法启动对用户位置的异步请求。如果请求成功,则运行方法中的回调函数。此函数使用点表示法访问<code>position</code>对象的纬度和经度值,并更新HTML。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">在<code>script</code>标记内添加示例代码以检查用户的当前位置并将其插入HTML。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 您的代码应使用<code>navigator.geolocation</code>来访问用户的当前位置。
|
||||
testString: 'assert(code.match(/navigator\.geolocation\.getCurrentPosition/g), "Your code should use <code>navigator.geolocation</code> to access the user's current location.");'
|
||||
- text: 您的代码应使用<code>position.coords.latitude</code>来显示用户的纬度位置。
|
||||
testString: 'assert(code.match(/position\.coords\.latitude/g), "Your code should use <code>position.coords.latitude</code> to display the user's latitudinal location.");'
|
||||
- text: 您的代码应使用<code>position.coords.longitude</code>来显示用户的纵向位置。
|
||||
testString: 'assert(code.match(/position\.coords\.longitude/g), "Your code should use <code>position.coords.longitude</code> to display the user's longitudinal location.");'
|
||||
- text: 您应该在<code>data</code> div元素中显示用户的位置。
|
||||
testString: 'assert(code.match(/document\.getElementById\(\s*?("|")data\1\s*?\)\.innerHTML/g), "You should display the user's position within the <code>data</code> div element.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='html-seed'>
|
||||
|
||||
```html
|
||||
<script>
|
||||
// Add your code below this line
|
||||
|
||||
|
||||
// Add your code above this line
|
||||
</script>
|
||||
<h4>You are here:</h4>
|
||||
<div id="data">
|
||||
|
||||
</div>
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,103 @@
|
||||
---
|
||||
id: 587d7fae367417b2b2512be3
|
||||
title: Get JSON with the JavaScript XMLHttpRequest Method
|
||||
challengeType: 6
|
||||
videoUrl: ''
|
||||
localeTitle: 使用JavaScript XMLHttpRequest方法获取JSON
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">您还可以从外部源请求数据。这就是API发挥作用的地方。请记住,API(或应用程序编程接口)是计算机用来相互通信的工具。您将学习如何使用我们从API获得的数据使用称为AJAX的技术更新HTML。大多数Web API以称为JSON的格式传输数据。 JSON代表JavaScript Object Notation。 JSON语法看起来与JavaScript对象文字表示法非常相似。 JSON具有对象属性及其当前值,夹在<code>{</code>和<code>}</code> 。这些属性及其值通常称为“键值对”。但是,API传输的JSON以<code>bytes</code>形式发送,应用程序将其作为<code>string</code>接收。这些可以转换为JavaScript对象,但默认情况下它们不是JavaScript对象。 <code>JSON.parse</code>方法解析字符串并构造它描述的JavaScript对象。您可以从freeCodeCamp的Cat Photo API请求JSON。以下是您可以在点击事件中添加的代码: <blockquote> req = new XMLHttpRequest(); <br> req.open( “GET”, '/ JSON / cats.json',TRUE); <br> req.send(); <br> req.onload =函数(){ <br> JSON = JSON.parse(req.responseText); <br> document.getElementsByClassName( '信息')[0] = .innerHTML JSON.stringify(JSON); <br> }; </blockquote>这是对每件作品的评论。 JavaScript <code>XMLHttpRequest</code>对象具有许多用于传输数据的属性和方法。首先,创建<code>XMLHttpRequest</code>对象的实例并将其保存在<code>req</code>变量中。接下来, <code>open</code>方法初始化一个请求 - 这个例子是从API请求数据,因此是一个“GET”请求。 <code>open</code>的第二个参数是您从中请求数据的API的URL。第三个参数是布尔值,其中<code>true</code>使其成为异步请求。 <code>send</code>方法发送请求。最后, <code>onload</code>事件处理程序解析返回的数据并应用<code>JSON.stringify</code>方法将JavaScript对象转换为字符串。然后将此字符串作为消息文本插入。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">更新代码以创建并向freeCodeCamp Cat Photo API发送“GET”请求。然后单击“获取消息”按钮。您的AJAX函数将使用API的原始JSON输出替换“消息将在此处”文本。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 您的代码应该创建一个新的<code>XMLHttpRequest</code> 。
|
||||
testString: 'assert(code.match(/new\s+?XMLHttpRequest\(\s*?\)/g), "Your code should create a new <code>XMLHttpRequest</code>.");'
|
||||
- text: 您的代码应该使用<code>open</code>方法初始化对freeCodeCamp Cat Photo API的“GET”请求。
|
||||
testString: 'assert(code.match(/\.open\(\s*?("|")GET\1\s*?,\s*?("|")\/json\/cats\.json\2\s*?,\s*?true\s*?\)/g), "Your code should use the <code>open</code> method to initialize a "GET" request to the freeCodeCamp Cat Photo API.");'
|
||||
- text: 您的代码应使用<code>send</code>方法发送请求。
|
||||
testString: 'assert(code.match(/\.send\(\s*\)/g), "Your code should use the <code>send</code> method to send the request.");'
|
||||
- text: 您的代码应该有一个设置为函数的<code>onload</code>事件处理程序。
|
||||
testString: 'assert(code.match(/\.onload\s*=\s*function\(\s*?\)\s*?{/g), "Your code should have an <code>onload</code> event handler set to a function.");'
|
||||
- text: 您的代码应该使用<code>JSON.parse</code>方法来解析<code>responseText</code> 。
|
||||
testString: 'assert(code.match(/JSON\.parse\(.*\.responseText\)/g), "Your code should use the <code>JSON.parse</code> method to parse the <code>responseText</code>.");'
|
||||
- text: 您的代码应该获取带有类<code>message</code>的元素,并将其内部HTML更改为JSON数据字符串。
|
||||
testString: 'assert(code.match(/document\.getElementsByClassName\(\s*?("|")message\1\s*?\)\[0\]\.innerHTML\s*?=\s*?JSON\.stringify\(.+?\)/g), "Your code should get the element with class <code>message</code> and change its inner HTML to the string of JSON data.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='html-seed'>
|
||||
|
||||
```html
|
||||
<script>
|
||||
document.addEventListener('DOMContentLoaded',function(){
|
||||
document.getElementById('getMessage').onclick=function(){
|
||||
// Add your code below this line
|
||||
|
||||
|
||||
// Add your code above this line
|
||||
};
|
||||
});
|
||||
</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>
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,93 @@
|
||||
---
|
||||
id: 587d7fad367417b2b2512be1
|
||||
title: Handle Click Events with JavaScript using the onclick property
|
||||
challengeType: 6
|
||||
videoUrl: ''
|
||||
localeTitle: 使用onclick属性处理使用JavaScript单击事件
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">您希望代码仅在页面加载完成后执行。为此,您可以将JavaScript事件附加到名为<code>DOMContentLoaded</code>的文档中。这是执行此操作的代码: <blockquote> document.addEventListener('DOMContentLoaded',function(){ <br><br> }); </blockquote>您可以实现<code>DOMContentLoaded</code>函数内部的事件处理程序。您可以通过添加以下代码来实现<code>onclick</code>事件处理程序,该处理程序在用户单击id为<code>getMessage</code>的元素时触发: <blockquote> 。的document.getElementById( '的getMessage')的onclick =函数(){}; </blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">在<code>DOMContentLoaded</code>函数内为id为<code>getMessage</code>的元素添加一个click事件处理程序。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 您的代码应使用<code>document.getElementById</code>方法来选择<code>getMessage</code>元素。
|
||||
testString: 'assert(code.match(/document\.getElementById\(\s*?("|")getMessage\1\s*?\)/g), "Your code should use the <code>document.getElementById</code> method to select the <code>getMessage</code> element.");'
|
||||
- text: 您的代码应添加<code>onclick</code>事件处理程序。
|
||||
testString: 'assert(typeof document.getElementById("getMessage").onclick === "function", "Your code should add an <code>onclick</code> event handler.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='html-seed'>
|
||||
|
||||
```html
|
||||
<script>
|
||||
document.addEventListener('DOMContentLoaded',function(){
|
||||
// Add your code below this line
|
||||
|
||||
|
||||
// Add your code above this line
|
||||
});
|
||||
</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>
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,108 @@
|
||||
---
|
||||
id: 587d7faf367417b2b2512be9
|
||||
title: Post Data with the JavaScript XMLHttpRequest Method
|
||||
challengeType: 6
|
||||
videoUrl: ''
|
||||
localeTitle: 使用JavaScript XMLHttpRequest方法发布数据
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">在前面的示例中,您从外部资源接收数据。您也可以将数据发送到外部资源,只要该资源支持AJAX请求并且您知道URL。 JavaScript的<code>XMLHttpRequest</code>方法也用于将数据发布到服务器。这是一个例子: <blockquote> req = new XMLHttpRequest(); <br> req.open( “POST”,网址,真实); <br> req.setRequestHeader( '内容 - 类型', '文本/纯'); <br> req.onreadystatechange =函数(){ <br> if(req.readyState == 4 && req.status == 200){ <br> document.getElementsByClassName( '信息')[0] = .innerHTML req.responseText; <br> } <br> }; <br> req.send(用户名); </blockquote>你以前见过其中几种方法。这里<code>open</code>方法将请求初始化为对外部资源的给定URL的“POST”,并使用<code>true</code>布尔值使其异步。 <code>setRequestHeader</code>方法设置HTTP请求标头的值,该标头包含有关发件人和请求的信息。它必须在<code>open</code>方法之后调用,但在<code>send</code>方法之前调用。这两个参数是标题的名称和要设置为该标题正文的值。接下来, <code>onreadystatechange</code>事件侦听器处理请求状态的更改。 <code>readyState</code>为4表示操作已完成, <code>status</code>为200表示该操作成功。文档的HTML可以更新。最后, <code>send</code>方法使用<code>userName</code>值发送请求,该值由用户在<code>input</code>字段中给出。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">更新代码以创建并发送“POST”请求。然后在输入框中输入您的姓名,然后单击“发送消息”。您的AJAX功能将取代“来自服务器的回复将在这里”。与服务器的回复。在这种情况下,你的名字附加“爱猫”。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 您的代码应该创建一个新的<code>XMLHttpRequest</code> 。
|
||||
testString: 'assert(code.match(/new\s+?XMLHttpRequest\(\s*?\)/g), "Your code should create a new <code>XMLHttpRequest</code>.");'
|
||||
- text: 您的代码应使用<code>open</code>方法初始化对服务器的“POST”请求。
|
||||
testString: 'assert(code.match(/\.open\(\s*?("|")POST\1\s*?,\s*?url\s*?,\s*?true\s*?\)/g), "Your code should use the <code>open</code> method to initialize a "POST" request to the server.");'
|
||||
- text: 您的代码应使用<code>setRequestHeader</code>方法。
|
||||
testString: 'assert(code.match(/\.setRequestHeader\(\s*?("|")Content-Type\1\s*?,\s*?("|")text\/plain\2\s*?\)/g), "Your code should use the <code>setRequestHeader</code> method.");'
|
||||
- text: 您的代码应该将<code>onreadystatechange</code>事件处理程序设置为函数。
|
||||
testString: 'assert(code.match(/\.onreadystatechange\s*?=/g), "Your code should have an <code>onreadystatechange</code> event handler set to a function.");'
|
||||
- text: 您的代码应该获取带有类<code>message</code>的元素,并将其内部HTML更改为<code>responseText</code> 。
|
||||
testString: 'assert(code.match(/document\.getElementsByClassName\(\s*?("|")message\1\s*?\)\[0\]\.innerHTML\s*?=\s*?.+?\.responseText/g), "Your code should get the element with class <code>message</code> and change its inner HTML to the <code>responseText</code>.");'
|
||||
- text: 您的代码应使用<code>send</code>方法。
|
||||
testString: 'assert(code.match(/\.send\(\s*?userName\s*?\)/g), "Your code should use the <code>send</code> method.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='html-seed'>
|
||||
|
||||
```html
|
||||
<script>
|
||||
document.addEventListener('DOMContentLoaded',function(){
|
||||
document.getElementById('sendMessage').onclick=function(){
|
||||
|
||||
var userName=document.getElementById('name').value;
|
||||
// Add your code below this line
|
||||
|
||||
|
||||
// Add your code above this line
|
||||
};
|
||||
});
|
||||
</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 Friends</h1>
|
||||
<p class="message box">
|
||||
Reply from Server will be here
|
||||
</p>
|
||||
<p>
|
||||
<label for="name">Your name:
|
||||
<input type="text" id="name"/>
|
||||
</label>
|
||||
<button id="sendMessage">
|
||||
Send Message
|
||||
</button>
|
||||
</p>
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,108 @@
|
||||
---
|
||||
id: 587d7fae367417b2b2512be7
|
||||
title: Pre-filter JSON to Get the Data You Need
|
||||
challengeType: 6
|
||||
videoUrl: ''
|
||||
localeTitle: 预过滤JSON以获取所需的数据
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">如果您不想渲染来自freeCodeCamp Cat Photo API的每张猫照片,您可以在循环之前预先过滤JSON。鉴于JSON数据存储在数组中,您可以使用<code>filter</code>方法过滤掉“id”键值为1的cat。以下是执行此操作的代码: <blockquote> json = json.filter(function(val){ <br> return(val.id!== 1); <br> }); </blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">添加代码以<code>filter</code> json数据以删除“id”值为1的cat。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 您的代码应该使用<code>filter</code>方法。
|
||||
testString: 'assert(code.match(/json\.filter/g), "Your code should use the <code>filter</code> method.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='html-seed'>
|
||||
|
||||
```html
|
||||
<script>
|
||||
document.addEventListener('DOMContentLoaded',function(){
|
||||
document.getElementById('getMessage').onclick=function(){
|
||||
req=new XMLHttpRequest();
|
||||
req.open("GET",'/json/cats.json',true);
|
||||
req.send();
|
||||
req.onload=function(){
|
||||
json=JSON.parse(req.responseText);
|
||||
var html = "";
|
||||
// Add your code below this line
|
||||
|
||||
|
||||
// Add your code above this line
|
||||
json.forEach(function(val) {
|
||||
html += "<div class = 'cat'>"
|
||||
|
||||
html += "<img src = '" + val.imageLink + "' " + "alt='" + val.altText + "'>"
|
||||
|
||||
html += "</div>"
|
||||
});
|
||||
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>
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,105 @@
|
||||
---
|
||||
id: 587d7fae367417b2b2512be6
|
||||
title: Render Images from Data Sources
|
||||
challengeType: 6
|
||||
videoUrl: ''
|
||||
localeTitle: 从数据源渲染图像
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">最后几个挑战表明,JSON数组中的每个对象都包含一个<code>imageLink</code>键,其值为cat图像的URL。循环浏览这些对象时,可以使用此<code>imageLink</code>属性在<code>img</code>元素中显示此图像。这是执行此操作的代码: <code>html += "<img src = '" + val.imageLink + "' " + "alt='" + val.altText + "'>";</code> </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">添加代码以在<code>img</code>标记中使用<code>imageLink</code>和<code>altText</code>属性。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 您应该使用<code>imageLink</code>属性来显示图像。
|
||||
testString: 'assert(code.match(/val\.imageLink/g), "You should use the <code>imageLink</code> property to display the images.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='html-seed'>
|
||||
|
||||
```html
|
||||
<script>
|
||||
document.addEventListener('DOMContentLoaded',function(){
|
||||
document.getElementById('getMessage').onclick=function(){
|
||||
req=new XMLHttpRequest();
|
||||
req.open("GET",'/json/cats.json',true);
|
||||
req.send();
|
||||
req.onload=function(){
|
||||
json=JSON.parse(req.responseText);
|
||||
var html = "";
|
||||
json.forEach(function(val) {
|
||||
html += "<div class = 'cat'>";
|
||||
// Add your code below this line
|
||||
|
||||
|
||||
// Add your code above this line
|
||||
html += "</div><br>";
|
||||
});
|
||||
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>
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
Reference in New Issue
Block a user