2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: 5a24c314108439a4d4036160
|
|
|
|
|
title: Define an HTML Class in JSX
|
|
|
|
|
challengeType: 6
|
2020-09-18 00:13:42 +08:00
|
|
|
|
forumTopicId: 301393
|
|
|
|
|
localeTitle: 在 JSX 中定义一个 HTML Class
|
2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
## Description
|
2020-09-18 00:13:42 +08:00
|
|
|
|
<section id='description'>
|
|
|
|
|
现在你已经习惯了编写 JSX,你可能想知道它与 HTML 有什么不同。
|
|
|
|
|
到目前为止,HTML 和 JSX 似乎完全相同。
|
|
|
|
|
JSX 的一个关键区别是你不能再使用<code>class</code>这个单词来定义 HTML 的 class 名。这是因为<code>class</code>是 JavaScript 中的关键字。JSX 使用<code>className</code>代替。
|
|
|
|
|
事实上,JSX 中所有 HTML 属性和事件引用的命名约定都变成了驼峰式。例如,JSX 中的单击事件是 <code>onClick</code>,而不是 <code>onclick</code>。同样,<code>onchange</code>变成了<code>onChange</code>。虽然这是一个微妙的差异,但请你一定要记住。
|
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
## Instructions
|
2020-09-18 00:13:42 +08:00
|
|
|
|
<section id='instructions'>
|
|
|
|
|
将 class<code>myDiv</code> 应用于 JSX 提供的<code>div</code>上。
|
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
## Tests
|
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
|
|
```yml
|
|
|
|
|
tests:
|
|
|
|
|
- text: 常量<code>JSX</code>应该返回一个<code>div</code>元素。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert.strictEqual(JSX.type, 'div');
|
2020-09-18 00:13:42 +08:00
|
|
|
|
- text: <code>div</code>有一个<code>myDiv</code>class。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert.strictEqual(JSX.props.className, 'myDiv');
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
|
|
<div id='jsx-seed'>
|
|
|
|
|
|
|
|
|
|
```jsx
|
|
|
|
|
const JSX = (
|
|
|
|
|
<div>
|
|
|
|
|
<h1>Add a class to this div</h1>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### After Test
|
|
|
|
|
<div id='jsx-teardown'>
|
|
|
|
|
|
|
|
|
|
```js
|
2020-09-18 00:13:42 +08:00
|
|
|
|
ReactDOM.render(JSX, document.getElementById('root'))
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
|
<section id='solution'>
|
|
|
|
|
|
2020-09-18 00:13:42 +08:00
|
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```js
|
2020-09-18 00:13:42 +08:00
|
|
|
|
const JSX = (
|
|
|
|
|
<div className = 'myDiv'>
|
|
|
|
|
<h1>Add a class to this div</h1>
|
|
|
|
|
</div>);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
2020-08-13 17:24:35 +02:00
|
|
|
|
|
2020-09-18 00:13:42 +08:00
|
|
|
|
</section>
|