2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: 587d7fad367417b2b2512be2
|
2020-12-16 00:37:30 -07:00
|
|
|
title: 通过单击事件更改文本
|
2018-10-10 18:03:03 -04:00
|
|
|
challengeType: 6
|
2020-09-07 16:18:38 +08:00
|
|
|
forumTopicId: 301500
|
2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --description--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
当点击事件发生时,你可以使用 JavaScript 更新 HTML 元素。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
例如,当用户点击 "Get Message" 按钮时,它将改变类名`message`元素的文本为 "Here is the message"。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
通过在点击事件内添加以下代码实现:
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
`document.getElementsByClassName('message')[0].textContent="Here is the message";`
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --instructions--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
在`onclick`事件处理器中添加代码,改变`message`元素内的文字为 "Here is the message"。
|
2020-09-07 16:18:38 +08:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --hints--
|
2020-09-07 16:18:38 +08:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
你的代码应该使用`document.getElementsByClassName`方法来选择类名为`message`的元素,然后将其`innerHTML`改为给定文字。
|
2020-09-07 16:18:38 +08:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
```js
|
|
|
|
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
|
|
|
```
|
2020-08-13 17:24:35 +02:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --solutions--
|
2020-09-07 16:18:38 +08:00
|
|
|
|