2018-10-10 18:03:03 -04:00
---
id: 587d7fad367417b2b2512be2
challengeType: 6
2020-09-07 16:18:38 +08:00
forumTopicId: 301500
2020-10-01 17:54:21 +02:00
title: 通过单击事件更改文本
2018-10-10 18:03:03 -04:00
---
## Description
2020-09-07 16:18:38 +08:00
< section id = 'description' >
当点击事件发生时,你可以使用 JavaScript 更新 HTML 元素。
例如,当用户点击 "Get Message" 按钮时,它将改变类名< code > message< / code > 元素的文本为 "Here is the message"。
通过在点击事件内添加以下代码实现:
< code > document.getElementsByClassName('message')[0].textContent="Here is the message";< / code >
< / section >
2018-10-10 18:03:03 -04:00
## Instructions
2020-09-07 16:18:38 +08:00
< section id = 'instructions' >
在< code > onclick< / code > 事件处理器中添加代码,改变< code > message< / code > 元素内的文字为 "Here is the message"。
< / section >
2018-10-10 18:03:03 -04:00
## Tests
< section id = 'tests' >
```yml
tests:
2020-09-07 16:18:38 +08:00
- text: 你的代码应该使用< code > document.getElementsByClassName</ code > 方法来选择类名为< code > message</ code > 的元素,然后将其< code > innerHTML</ code > 改为给定文字。
2020-02-18 01:40:55 +09:00
testString: assert(code.match(/document\s*\.getElementsByClassName\(\s*?('|")message\1\s*?\)\[0\]\s*\.textContent\s*?=\s*?('|")Here is the message\2/g));
2018-10-10 18:03:03 -04:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'html-seed' >
```html
< script >
2020-09-07 16:18:38 +08:00
document.addEventListener('DOMContentLoaded', function(){
document.getElementById('getMessage').onclick = function(){
// 在这行下面添加代码
2018-10-10 18:03:03 -04:00
2020-09-07 16:18:38 +08:00
// 在这行上面添加代码
2018-10-10 18:03:03 -04:00
}
});
< / script >
2020-09-07 16:18:38 +08:00
2018-10-10 18:03:03 -04:00
< 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 >
2020-09-07 16:18:38 +08:00
< h1 > Cat Photo Finder< / h1 >
< p class = "message box" >
2018-10-10 18:03:03 -04:00
The message will go here
< / p >
< p >
< button id = "getMessage" >
Get Message
< / button >
< / p >
```
< / div >
< / section >
## Solution
< section id = 'solution' >
2020-09-07 16:18:38 +08:00
```html
< script >
document.addEventListener('DOMContentLoaded',function(){
document.getElementById('getMessage').onclick = function(){
// Add your code below this line
document.getElementsByClassName('message')[0].textContent = "Here is the message";
// 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" >
The message will go here
< / p >
< p >
< button id = "getMessage" >
Get Message
< / button >
< / p >
2018-10-10 18:03:03 -04:00
```
2020-08-13 17:24:35 +02:00
2020-09-07 16:18:38 +08:00
< / section >