2018-10-10 22:03:03 +00:00
|
|
|
---
|
|
|
|
id: bad87fee1348bd9aedf08830
|
2020-12-16 07:37:30 +00:00
|
|
|
title: 给输入框添加占位符文本
|
2018-10-10 22:03:03 +00:00
|
|
|
challengeType: 0
|
2019-12-26 12:05:59 +00:00
|
|
|
videoUrl: 'https://scrimba.com/p/pVMPUv/cKdJDhg'
|
|
|
|
forumTopicId: 16647
|
2021-01-13 02:31:00 +00:00
|
|
|
dashedName: add-placeholder-text-to-a-text-field
|
2018-10-10 22:03:03 +00:00
|
|
|
---
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --description--
|
|
|
|
|
2021-01-21 02:58:05 +00:00
|
|
|
占位符文本用户在 `input` 输入框中输入任何东西前的预定义文本。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
2019-12-26 12:05:59 +00:00
|
|
|
你可以像这样创建一个占位符:
|
2020-12-16 07:37:30 +00:00
|
|
|
|
2021-03-21 16:58:20 +00:00
|
|
|
```html
|
|
|
|
<input type="text" placeholder="this is placeholder text">
|
|
|
|
```
|
2020-12-16 07:37:30 +00:00
|
|
|
|
2021-08-25 16:12:11 +00:00
|
|
|
**注意:** 别忘了 `input` 元素是 "自闭和标签",即不需要结束标签。
|
2021-01-08 19:20:48 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --instructions--
|
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
把 `input` 输入框的 `placeholder` 占位符文本设置为 "cat photo URL"。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
|
|
# --hints--
|
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
给现有的 `input` 输入框添加一个 `placeholder` 属性。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert($('input[placeholder]').length > 0);
|
2018-10-10 22:03:03 +00:00
|
|
|
```
|
|
|
|
|
2021-02-06 04:42:36 +00:00
|
|
|
设置 `placeholder` 属性的值为 `cat photo URL`。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert(
|
|
|
|
$('input') &&
|
|
|
|
$('input').attr('placeholder') &&
|
|
|
|
$('input')
|
|
|
|
.attr('placeholder')
|
2021-01-08 19:20:48 +00:00
|
|
|
.match(/cat\s+photo\s+URL/gi)
|
2020-12-16 07:37:30 +00:00
|
|
|
);
|
2018-10-10 22:03:03 +00:00
|
|
|
```
|
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
`input` 元素不该有结束标签。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
assert(!code.match(/<input.*\/?>.*<\/input>/gi));
|
|
|
|
```
|
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
`input` 输入框的语法必须正确。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
assert($('input[type=text]').length > 0);
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
# --seed--
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
```html
|
|
|
|
<h2>CatPhotoApp</h2>
|
|
|
|
<main>
|
|
|
|
<p>Click here to view more <a href="#">cat photos</a>.</p>
|
|
|
|
|
2021-09-22 15:34:59 +00:00
|
|
|
<a href="#"><img src="https://cdn.freecodecamp.org/curriculum/cat-photo-app/relaxing-cat.jpg" alt="A cute orange cat lying on its back."></a>
|
2021-01-13 02:31:00 +00:00
|
|
|
|
|
|
|
<p>Things cats love:</p>
|
|
|
|
<ul>
|
|
|
|
<li>cat nip</li>
|
|
|
|
<li>laser pointers</li>
|
|
|
|
<li>lasagna</li>
|
|
|
|
</ul>
|
|
|
|
<p>Top 3 things cats hate:</p>
|
|
|
|
<ol>
|
|
|
|
<li>flea treatment</li>
|
|
|
|
<li>thunder</li>
|
|
|
|
<li>other cats</li>
|
|
|
|
</ol>
|
|
|
|
<input type="text">
|
|
|
|
</main>
|
|
|
|
```
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --solutions--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
```html
|
|
|
|
<h2>CatPhotoApp</h2>
|
|
|
|
<main>
|
|
|
|
<p>Click here to view more <a href="#">cat photos</a>.</p>
|
2021-01-21 02:58:05 +00:00
|
|
|
|
2021-09-22 15:34:59 +00:00
|
|
|
<a href="#"><img src="https://cdn.freecodecamp.org/curriculum/cat-photo-app/relaxing-cat.jpg" alt="A cute orange cat lying on its back."></a>
|
2021-01-21 02:58:05 +00:00
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
<p>Things cats love:</p>
|
|
|
|
<ul>
|
|
|
|
<li>cat nip</li>
|
|
|
|
<li>laser pointers</li>
|
|
|
|
<li>lasagna</li>
|
|
|
|
</ul>
|
|
|
|
<p>Top 3 things cats hate:</p>
|
|
|
|
<ol>
|
|
|
|
<li>flea treatment</li>
|
|
|
|
<li>thunder</li>
|
|
|
|
<li>other cats</li>
|
|
|
|
</ol>
|
|
|
|
<input type="text" placeholder="cat photo URL">
|
|
|
|
</main>
|
|
|
|
```
|