Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOM の章の確認問題を追加、内容のアップデート #725

Merged
merged 9 commits into from
May 14, 2024
16 changes: 16 additions & 0 deletions docs/1-trial-session/13-dom/_samples/add-strong-snack/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!doctype html>
<html lang="ja">
<head>
<meta charset="utf-8" />
<title>Title</title>
</head>
<body>
<ul id="packing-list">
<li>お弁当</li>
<li>水筒</li>
<li>タオル</li>
<li>レジャーシート</li>
</ul>
<script src="./script.js"></script>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const newItem = document.createElement("li");
const strongPart = document.createElement("strong");
strongPart.textContent = "おやつ";
newItem.appendChild(strongPart);

const packingList = document.getElementById("packing-list");
packingList.appendChild(newItem);
11 changes: 11 additions & 0 deletions docs/1-trial-session/13-dom/_samples/change-styles/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!doctype html>
<html lang="ja">
<head>
<meta charset="utf-8" />
<title>Title</title>
</head>
<body>
<div id="greeting">Hello World!</div>
<script src="./script.js"></script>
</body>
</html>
3 changes: 3 additions & 0 deletions docs/1-trial-session/13-dom/_samples/change-styles/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const greetingElement = document.getElementById("greeting");
greetingElement.style.color = "yellow";
greetingElement.style.backgroundColor = "black";
11 changes: 11 additions & 0 deletions docs/1-trial-session/13-dom/_samples/good-evening/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!doctype html>
<html lang="ja">
<head>
<meta charset="utf-8" />
<title>Title</title>
</head>
<body>
<div>Good <strong id="greeting-type">morning</strong>!</div>
<script src="./script.js"></script>
</body>
</html>
2 changes: 2 additions & 0 deletions docs/1-trial-session/13-dom/_samples/good-evening/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const greetingType = document.getElementById("greeting-type");
greetingType.textContent = "evening";
Binary file added docs/1-trial-session/13-dom/add-strong-snack.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
103 changes: 99 additions & 4 deletions docs/1-trial-session/13-dom/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,28 @@ title: DOM
`document.getElementById` <Term>関数</Term>は、<Term>引数</Term>として <Term>HTML 要素</Term>の `id` <Term>属性</Term>に指定された値を<Term>文字列</Term>として<Term>渡す</Term>ことで、その<Term>要素</Term>を表す<Term>オブジェクト</Term>を<Term>返し</Term>ます。

```html title="index.html"
<div id="greeting">Hello World</div>
<!doctype html>
<html lang="ja">
<head>
<meta charset="utf-8" />
<title>Title</title>
</head>
<body>
<div id="greeting">Hello World</div>
<script src="./script.js"></script>
</body>
</html>
```

```js title="script.js"
const element = document.getElementById("greeting");
element.textContent = "Hello DOM";
const greetingElement = document.getElementById("greeting");
greetingElement.textContent = "Hello DOM";
```

<ViewSource url={import.meta.url} path="_samples/get-element-by-id" />

{/* prettier-ignore */}
<Term>変数</Term> `element` には、`index.html` に記述された `div` <Term>要素</Term>に対応する<Term>オブジェクト</Term>が<Term>代入</Term>されています。
<Term>変数</Term> `greetingElement` には、`index.html` に記述された `div` <Term>要素</Term>に対応する<Term>オブジェクト</Term>が<Term>代入</Term>されています。

![DOM](./dom.png)

Expand All @@ -37,6 +47,34 @@ element.textContent = "Hello DOM";

`textContent` <Term>プロパティ</Term>は、<Term>HTML 要素</Term>の内部のテキストを表します。<Term>オブジェクト</Term>の<Term>プロパティ</Term>は通常の<Term>変数</Term>のように取得や<Term>代入</Term>が可能で、上の例では `textContent` <Term>プロパティ</Term>に対して `"Hello DOM"` という<Term>文字列</Term>を代入することで、`div` <Term>要素</Term>の内部のテキストを変更しています。

### 確認問題

下のような HTML が記述されています。

```html title="index.html"
<div>Good <strong id="greeting-type">morning</strong>!</div>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これ greeting-type で英語的に合ってるんでしょうか?
greeting-time とかの方が良さそうに見えます。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://learn.utcode.net/docs/trial-session/functions/#引数
に倣いましたが、確かにそうですね……

Copy link
Contributor

@chvmvd chvmvd May 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

greeting-typeだと有限のものの1つで、greeting-timeだと無限のもの(連続のもの)の1つという感覚を受けそうですね。
今回だとmorningafternooneveningみたいな感じで有限なもののどれかということを表しているので、greetingTypeだと明確かなと思いました。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

そしたらとりあえずこのままにします。問題があれば後で両方変えましょう

```

JavaScript を使って、`Good evening!` (`evening` は太字) と表示されるようにしてください。
chvmvd marked this conversation as resolved.
Show resolved Hide resolved

:::warning

- 読みやすさのため、`body` 要素の内部の、さらに `script` 要素以外の部分だけを抽出して表記しています。実際にはそれらの部分を含めて記述する必要があるので注意してください。これ以降もこのように省略して記述します。
- `script` 要素は `body` 要素の内部のいちばん下に配置するようにしてください。

:::
chvmvd marked this conversation as resolved.
Show resolved Hide resolved

<Answer>

```js title="script.js"
const greetingType = document.getElementById("greeting-type");
greetingType.textContent = "evening";
```

<ViewSource url={import.meta.url} path="_samples/good-evening" />

</Answer>

## <Term>HTML 要素</Term>のスタイルを変更する

`document.getElementById` <Term>関数</Term>が返す<Term>オブジェクト</Term>の `style` <Term>プロパティ</Term>は、その要素の <Term>`style` 属性</Term>と対応します。 **`style` <Term>プロパティ</Term>に格納されている<Term>値</Term>自体も<Term>オブジェクト</Term>** となっており、その各<Term>プロパティ</Term>が CSS の<Term type="cssProperty">プロパティ</Term>に対応します。
Expand All @@ -53,6 +91,30 @@ element.style.backgroundColor = "red";
<Term>CSS</Term> の<Term type="cssProperty">プロパティ</Term>名である `background-color` は、内部にハイフンが含まれているため、`element.style.background-color` のように指定してしまうと、ハイフンが減算<Term>演算子</Term>として解釈されてしまいます。
`style` <Term>プロパティ</Term>では、<Term>CSS</Term> の<Term type="cssProperty">プロパティ</Term>名は<Term>キャメルケース</Term>として指定する必要があることに注意してください。

### 確認問題

下のような HTML ファイルがあります。

```html title="index.html"
<div id="greeting">Hello World!</div>
```

JavaScript を使って、文字色が黄色、背景色が好きな色 (この例では黒) で `Hello World!` が表示されるようにしてください。
chvmvd marked this conversation as resolved.
Show resolved Hide resolved

![文字色が黄色、背景色が黒色の Hello World!](./yellow-hello-world.png)

<Answer>

```js title="script.js"
const greetingElement = document.getElementById("greeting");
greetingElement.style.color = "yellow";
greetingElement.style.backgroundColor = "black";
```

<ViewSource url={import.meta.url} path="_samples/change-styles" />

</Answer>

## DOM を用いて要素を追加する

`document.createElement` 関数は、引数に要素の種類を表す文字列を取り、その種類の新しい HTML 要素を作る関数です。
Expand Down Expand Up @@ -88,6 +150,39 @@ packingList.appendChild(newItem);

これで、既存の `ul` 要素の子要素に新しい `li` 要素が追加され、「おやつ」が加わった持ち物リストが表示されます。

### 確認問題

遠足の持ち物リストがあります。

```html title="index.html"
<ul id="packing-list">
<li>お弁当</li>
<li>水筒</li>
<li>タオル</li>
<li>レジャーシート</li>
</ul>
```

このリストに新しい項目として「おやつ」を追加してください。ただし、「おやつ」は `strong` 要素を用いて太字で表示されるようにしてください。
chvmvd marked this conversation as resolved.
Show resolved Hide resolved

![おやつが太字になった持ち物リスト](./add-strong-snack.png)

<Answer>

```js title="script.js"
const newItem = document.createElement("li");
const strongPart = document.createElement("strong");
strongPart.textContent = "おやつ";
newItem.appendChild(strongPart);

const packingList = document.getElementById("packing-list");
packingList.appendChild(newItem);
```

<ViewSource url={import.meta.url} path="_samples/add-snack" />

</Answer>

## 初級演習

### 買い物リストの書き換え
chvmvd marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading