diff --git a/.prettierignore b/.prettierignore index 93ef1c28e73f10..50dc02b28a883d 100644 --- a/.prettierignore +++ b/.prettierignore @@ -40,7 +40,6 @@ build/ /files/es/web/javascript/reference/**/*.md # ja -/files/ja/learn/javascript/**/*.md /files/ja/mozilla/**/*.md /files/ja/mdn/**/*.md /files/ja/mozilla/add-ons/**/*.md diff --git a/files/ja/learn/javascript/asynchronous/implementing_a_promise-based_api/index.md b/files/ja/learn/javascript/asynchronous/implementing_a_promise-based_api/index.md index 2f7e70e101cea3..134e15b2da1ecc 100644 --- a/files/ja/learn/javascript/asynchronous/implementing_a_promise-based_api/index.md +++ b/files/ja/learn/javascript/asynchronous/implementing_a_promise-based_api/index.md @@ -48,16 +48,16 @@ div { ``` ```js -const output = document.querySelector('#output'); -const button = document.querySelector('#set-alarm'); +const output = document.querySelector("#output"); +const button = document.querySelector("#set-alarm"); function setAlarm() { setTimeout(() => { - output.textContent = '起きて!'; + output.textContent = "起きて!"; }, 1000); } -button.addEventListener('click', setAlarm); +button.addEventListener("click", setAlarm); ``` {{EmbedLiveSample("Wrapping setTimeout()", 600, 100)}} @@ -76,7 +76,7 @@ button.addEventListener('click', setAlarm); function alarm(person, delay) { return new Promise((resolve, reject) => { if (delay < 0) { - throw new Error('アラームの待ち時間を負数にすることはできません。'); + throw new Error("アラームの待ち時間を負数にすることはできません。"); } setTimeout(() => { resolve(`${person}、起きて!`); @@ -122,15 +122,15 @@ button { ``` ```js -const name = document.querySelector('#name'); -const delay = document.querySelector('#delay'); -const button = document.querySelector('#set-alarm'); -const output = document.querySelector('#output'); +const name = document.querySelector("#name"); +const delay = document.querySelector("#delay"); +const button = document.querySelector("#set-alarm"); +const output = document.querySelector("#output"); function alarm(person, delay) { return new Promise((resolve, reject) => { if (delay < 0) { - throw new Error('アラームの待ち時間を負数にすることはできません。'); + throw new Error("アラームの待ち時間を負数にすることはできません。"); } setTimeout(() => { resolve(`${person}、起きて!`); @@ -138,10 +138,12 @@ function alarm(person, delay) { }); } -button.addEventListener('click', () => { +button.addEventListener("click", () => { alarm(name.value, delay.value) - .then((message) => output.textContent = message) - .catch((error) => output.textContent = `アラームを設定できません: ${error}`); + .then((message) => (output.textContent = message)) + .catch( + (error) => (output.textContent = `アラームを設定できません: ${error}`), + ); }); ``` @@ -180,15 +182,15 @@ button { ``` ```js -const name = document.querySelector('#name'); -const delay = document.querySelector('#delay'); -const button = document.querySelector('#set-alarm'); -const output = document.querySelector('#output'); +const name = document.querySelector("#name"); +const delay = document.querySelector("#delay"); +const button = document.querySelector("#set-alarm"); +const output = document.querySelector("#output"); function alarm(person, delay) { return new Promise((resolve, reject) => { if (delay < 0) { - throw new Error('アラームの待ち時間を負数にすることはできません。'); + throw new Error("アラームの待ち時間を負数にすることはできません。"); } setTimeout(() => { resolve(`${person}、起きて!`); @@ -196,12 +198,11 @@ function alarm(person, delay) { }); } -button.addEventListener('click', async () => { +button.addEventListener("click", async () => { try { const message = await alarm(name.value, delay.value); output.textContent = message; - } - catch (error) { + } catch (error) { output.textContent = `アラームを設定できません: ${error}`; } }); diff --git a/files/ja/learn/javascript/asynchronous/introducing/index.md b/files/ja/learn/javascript/asynchronous/introducing/index.md index 015831174bea64..45b13ae4eaf4ac 100644 --- a/files/ja/learn/javascript/asynchronous/introducing/index.md +++ b/files/ja/learn/javascript/asynchronous/introducing/index.md @@ -43,7 +43,7 @@ l10n: 以下のようなコードを考えてみましょう。 ```js -const name = 'Miriam'; +const name = "Miriam"; const greeting = `Hello, my name is ${name}!`; console.log(greeting); // "Hello, my name is Miriam!" @@ -64,7 +64,7 @@ function makeGreeting(name) { return `Hello, my name is ${name}!`; } -const name = 'Miriam'; +const name = "Miriam"; const greeting = makeGreeting(name); console.log(greeting); // "Hello, my name is Miriam!" @@ -113,15 +113,15 @@ function generatePrimes(quota) { return primes; } -const quota = document.querySelector('#quota'); -const output = document.querySelector('#output'); +const quota = document.querySelector("#quota"); +const output = document.querySelector("#output"); -document.querySelector('#generate').addEventListener('click', () => { +document.querySelector("#generate").addEventListener("click", () => { const primes = generatePrimes(quota.value); output.textContent = `${quota.value} 個の素数を生成しました。`; }); -document.querySelector('#reload').addEventListener('click', () => { +document.querySelector("#reload").addEventListener("click", () => { document.location.reload(); }); ``` @@ -182,15 +182,15 @@ function generatePrimes(quota) { return primes; } -const quota = document.querySelector('#quota'); -const output = document.querySelector('#output'); +const quota = document.querySelector("#quota"); +const output = document.querySelector("#output"); -document.querySelector('#generate').addEventListener('click', () => { +document.querySelector("#generate").addEventListener("click", () => { const primes = generatePrimes(quota.value); output.textContent = `${quota.value} 個の素数を生成しました。`; }); -document.querySelector('#reload').addEventListener('click', () => { +document.querySelector("#reload").addEventListener("click", () => { document.location.reload(); }); ``` @@ -230,23 +230,27 @@ pre { ``` ```js -const log = document.querySelector('.event-log'); +const log = document.querySelector(".event-log"); -document.querySelector('#xhr').addEventListener('click', () => { - log.textContent = ''; +document.querySelector("#xhr").addEventListener("click", () => { + log.textContent = ""; const xhr = new XMLHttpRequest(); - xhr.addEventListener('loadend', () => { + xhr.addEventListener("loadend", () => { log.textContent = `${log.textContent}ステータス ${xhr.status} で完了しました`; }); - xhr.open('GET', 'https://raw.githubusercontent.com/mdn/content/main/files/en-us/_wikihistory.json'); + xhr.open( + "GET", + "https://raw.githubusercontent.com/mdn/content/main/files/en-us/_wikihistory.json", + ); xhr.send(); - log.textContent = `${log.textContent}XHR リクエストを開始しました\n`;}); + log.textContent = `${log.textContent}XHR リクエストを開始しました\n`; +}); -document.querySelector('#reload').addEventListener('click', () => { - log.textContent = ''; +document.querySelector("#reload").addEventListener("click", () => { + log.textContent = ""; document.location.reload(); }); ``` diff --git a/files/ja/learn/javascript/asynchronous/introducing_workers/index.md b/files/ja/learn/javascript/asynchronous/introducing_workers/index.md index 95bbeb250bc16c..7aa03e98c30f60 100644 --- a/files/ja/learn/javascript/asynchronous/introducing_workers/index.md +++ b/files/ja/learn/javascript/asynchronous/introducing_workers/index.md @@ -50,12 +50,11 @@ l10n: ```js function generatePrimes(quota) { - function isPrime(n) { for (let c = 2; c <= Math.sqrt(n); ++c) { if (n % c === 0) { - return false; - } + return false; + } } return true; } @@ -73,14 +72,17 @@ function generatePrimes(quota) { return primes; } -document.querySelector('#generate').addEventListener('click', () => { - const quota = document.querySelector('#quota').value; +document.querySelector("#generate").addEventListener("click", () => { + const quota = document.querySelector("#quota").value; const primes = generatePrimes(quota); - document.querySelector('#output').textContent = `Finished generating ${quota} primes!`; + document.querySelector( + "#output", + ).textContent = `Finished generating ${quota} primes!`; }); -document.querySelector('#reload').addEventListener('click', () => { - document.querySelector('#user-input').value = 'Try typing in here immediately after pressing "Generate primes"'; +document.querySelector("#reload").addEventListener("click", () => { + document.querySelector("#user-input").value = + 'Try typing in here immediately after pressing "Generate primes"'; document.location.reload(); }); ``` @@ -99,7 +101,7 @@ document.querySelector('#reload').addEventListener('click', () => { "index.html" ファイルと "style.css" ファイルは、すでに完成しています。 ```html - + @@ -118,7 +120,8 @@ document.querySelector('#reload').addEventListener('click', () => { +
@@ -140,15 +143,15 @@ textarea { ```js // 新しいワーカーを作成し、"generate.js" にあるコードを与えます。 -const worker = new Worker('./generate.js'); +const worker = new Worker("./generate.js"); // ユーザーが[素数の生成]をクリックしたら、ワーカーにメッセージを送ります。 // メッセージのコマンドは "generate" であり、メッセージには生成する素数の // 数である "quota" も含まれています。 -document.querySelector('#generate').addEventListener('click', () => { - const quota = document.querySelector('#quota').value; +document.querySelector("#generate").addEventListener("click", () => { + const quota = document.querySelector("#quota").value; worker.postMessage({ - command: 'generate', + command: "generate", quota, }); }); @@ -156,12 +159,15 @@ document.querySelector('#generate').addEventListener('click', () => { // ワーカーがメインスレッドにメッセージを送り返したら、メッセージ // データから受け取った生成された素数の個数を含むユーザーへの // メッセージで出力ボックスを更新します。 -worker.addEventListener('message', (message) => { - document.querySelector('#output').textContent = `${message.data} 個の素数を生成しました。`; +worker.addEventListener("message", (message) => { + document.querySelector( + "#output", + ).textContent = `${message.data} 個の素数を生成しました。`; }); -document.querySelector('#reload').addEventListener('click', () => { - document.querySelector('#user-input').value = '[素数の生成]を押した後、すぐにここに入力してみてください。'; +document.querySelector("#reload").addEventListener("click", () => { + document.querySelector("#user-input").value = + "[素数の生成]を押した後、すぐにここに入力してみてください。"; document.location.reload(); }); ``` @@ -183,19 +189,18 @@ document.querySelector('#reload').addEventListener('click', () => { // メインスレッドからのメッセージを待ち受けします。 // メッセージのコマンドが "generate" であれば、 `generatePrimes()` を呼び出します。 addEventListener("message", (message) => { - if (message.data.command === 'generate') { + if (message.data.command === "generate") { generatePrimes(message.data.quota); } }); // Generate primes (very inefficiently) function generatePrimes(quota) { - function isPrime(n) { for (let c = 2; c <= Math.sqrt(n); ++c) { if (n % c === 0) { - return false; - } + return false; + } } return true; } diff --git a/files/ja/learn/javascript/asynchronous/promises/index.md b/files/ja/learn/javascript/asynchronous/promises/index.md index f66f4fd131cd8b..20eddd4d6b7e4a 100644 --- a/files/ja/learn/javascript/asynchronous/promises/index.md +++ b/files/ja/learn/javascript/asynchronous/promises/index.md @@ -43,7 +43,9 @@ l10n: これをブラウザーの JavaScript コンソールにコピーしてください。 ```js -const fetchPromise = fetch('https://mdn.github.io/learning-area/javascript/apis/fetching-data/can-store/products.json'); +const fetchPromise = fetch( + "https://mdn.github.io/learning-area/javascript/apis/fetching-data/can-store/products.json", +); console.log(fetchPromise); @@ -80,7 +82,9 @@ Promise { : "pending" } これを実行してみましょう。 ```js -const fetchPromise = fetch('https://mdn.github.io/learning-area/javascript/apis/fetching-data/can-store/products.json'); +const fetchPromise = fetch( + "https://mdn.github.io/learning-area/javascript/apis/fetching-data/can-store/products.json", +); fetchPromise.then((response) => { const jsonPromise = response.json(); @@ -99,7 +103,9 @@ fetchPromise.then((response) => { もちろんそうです。しかし、プロミスのエレガントな特徴は、_`then()` 自身がプロミスを返し、それに渡された関数の結果で完了することです_。これはつまり、上記のコードをこのように書き換えることができる(そして、確かにそうすべき)ということです。 ```js -const fetchPromise = fetch('https://mdn.github.io/learning-area/javascript/apis/fetching-data/can-store/products.json'); +const fetchPromise = fetch( + "https://mdn.github.io/learning-area/javascript/apis/fetching-data/can-store/products.json", +); fetchPromise .then((response) => response.json()) @@ -113,7 +119,9 @@ fetchPromise 次のステップに移動する前に、もう一つ追加しなければならないことがあります。リクエストを読み込む前に、サーバーがリクエストを受け入れ、処理することができたかどうかを調べる必要があります。これを行うには、レスポンスのステータスコードを調べて、それが "OK" でない場合はエラーを発生させます。 ```js -const fetchPromise = fetch('https://mdn.github.io/learning-area/javascript/apis/fetching-data/can-store/products.json'); +const fetchPromise = fetch( + "https://mdn.github.io/learning-area/javascript/apis/fetching-data/can-store/products.json", +); fetchPromise .then((response) => { @@ -140,7 +148,9 @@ fetchPromise このバージョンの `fetch()` コードを試してみてください。 `catch()` を使用したエラーハンドラーを追加し、リクエストが失敗するように URL も変更しています。 ```js -const fetchPromise = fetch('bad-scheme://mdn.github.io/learning-area/javascript/apis/fetching-data/can-store/products.json'); +const fetchPromise = fetch( + "bad-scheme://mdn.github.io/learning-area/javascript/apis/fetching-data/can-store/products.json", +); fetchPromise .then((response) => { @@ -191,9 +201,15 @@ fetchPromise 例を示します。 ```js -const fetchPromise1 = fetch('https://mdn.github.io/learning-area/javascript/apis/fetching-data/can-store/products.json'); -const fetchPromise2 = fetch('https://mdn.github.io/learning-area/javascript/apis/fetching-data/can-store/not-found'); -const fetchPromise3 = fetch('https://mdn.github.io/learning-area/javascript/oojs/json/superheroes.json'); +const fetchPromise1 = fetch( + "https://mdn.github.io/learning-area/javascript/apis/fetching-data/can-store/products.json", +); +const fetchPromise2 = fetch( + "https://mdn.github.io/learning-area/javascript/apis/fetching-data/can-store/not-found", +); +const fetchPromise3 = fetch( + "https://mdn.github.io/learning-area/javascript/oojs/json/superheroes.json", +); Promise.all([fetchPromise1, fetchPromise2, fetchPromise3]) .then((responses) => { @@ -202,7 +218,7 @@ Promise.all([fetchPromise1, fetchPromise2, fetchPromise3]) } }) .catch((error) => { - console.error(`Failed to fetch: ${error}`) + console.error(`Failed to fetch: ${error}`); }); ``` @@ -219,9 +235,15 @@ https://mdn.github.io/learning-area/javascript/oojs/json/superheroes.json: 200 同じコードを間違った形の URL で試すと、次のようになります。 ```js -const fetchPromise1 = fetch('https://mdn.github.io/learning-area/javascript/apis/fetching-data/can-store/products.json'); -const fetchPromise2 = fetch('https://mdn.github.io/learning-area/javascript/apis/fetching-data/can-store/not-found'); -const fetchPromise3 = fetch('bad-scheme://mdn.github.io/learning-area/javascript/oojs/json/superheroes.json'); +const fetchPromise1 = fetch( + "https://mdn.github.io/learning-area/javascript/apis/fetching-data/can-store/products.json", +); +const fetchPromise2 = fetch( + "https://mdn.github.io/learning-area/javascript/apis/fetching-data/can-store/not-found", +); +const fetchPromise3 = fetch( + "bad-scheme://mdn.github.io/learning-area/javascript/oojs/json/superheroes.json", +); Promise.all([fetchPromise1, fetchPromise2, fetchPromise3]) .then((responses) => { @@ -230,7 +252,7 @@ Promise.all([fetchPromise1, fetchPromise2, fetchPromise3]) } }) .catch((error) => { - console.error(`Failed to fetch: ${error}`) + console.error(`Failed to fetch: ${error}`); }); ``` @@ -243,16 +265,22 @@ Failed to fetch: TypeError: Failed to fetch 時には、設定されたプロミスのうちどれかが履行される必要があり、どれが履行されるかは気にしないことがあるかもしれません。そのような場合は {{jsxref("Promise/any", "Promise.any()")}} を指定します。これは `Promise.all()` と似ていますが、プロミスの配列のいずれかが履行されるとすぐに履行され、すべてが拒否されると拒否される点が異なります。 ```js -const fetchPromise1 = fetch('https://mdn.github.io/learning-area/javascript/apis/fetching-data/can-store/products.json'); -const fetchPromise2 = fetch('https://mdn.github.io/learning-area/javascript/apis/fetching-data/can-store/not-found'); -const fetchPromise3 = fetch('https://mdn.github.io/learning-area/javascript/oojs/json/superheroes.json'); +const fetchPromise1 = fetch( + "https://mdn.github.io/learning-area/javascript/apis/fetching-data/can-store/products.json", +); +const fetchPromise2 = fetch( + "https://mdn.github.io/learning-area/javascript/apis/fetching-data/can-store/not-found", +); +const fetchPromise3 = fetch( + "https://mdn.github.io/learning-area/javascript/oojs/json/superheroes.json", +); Promise.any([fetchPromise1, fetchPromise2, fetchPromise3]) .then((response) => { console.log(`${response.url}: ${response.status}`); }) .catch((error) => { - console.error(`Failed to fetch: ${error}`) + console.error(`Failed to fetch: ${error}`); }); ``` @@ -279,7 +307,9 @@ async function fetchProducts() { try { // この行の後、この関数は `fetch()` 呼び出しが決定されるのを待ちます。 // `fetch()` 呼び出しは Response を返すか、エラーを発生させます。 - const response = await fetch('https://mdn.github.io/learning-area/javascript/apis/fetching-data/can-store/products.json'); + const response = await fetch( + "https://mdn.github.io/learning-area/javascript/apis/fetching-data/can-store/products.json", + ); if (!response.ok) { throw new Error(`HTTP error: ${response.status}`); } @@ -287,8 +317,7 @@ async function fetchProducts() { // `response.json()` 呼び出しは、解釈された JSON オブジェクトを返すか、エラーを発生させるかのどちらかです。 const data = await response.json(); console.log(data[0].name); - } - catch (error) { + } catch (error) { console.error(`Could not get products: ${error}`); } } @@ -305,20 +334,21 @@ fetchProducts(); ```js example-bad async function fetchProducts() { try { - const response = await fetch('https://mdn.github.io/learning-area/javascript/apis/fetching-data/can-store/products.json'); + const response = await fetch( + "https://mdn.github.io/learning-area/javascript/apis/fetching-data/can-store/products.json", + ); if (!response.ok) { throw new Error(`HTTP error: ${response.status}`); } const data = await response.json(); return data; - } - catch (error) { + } catch (error) { console.error(`Could not get products: ${error}`); } } const promise = fetchProducts(); -console.log(promise[0].name); // "promise" は Promise オブジェクトなので、これは動作しません。 +console.log(promise[0].name); // "promise" は Promise オブジェクトなので、これは動作しません。 ``` その代わり、次のようにする必要があります。 @@ -326,14 +356,15 @@ console.log(promise[0].name); // "promise" は Promise オブジェクトな ```js async function fetchProducts() { try { - const response = await fetch('https://mdn.github.io/learning-area/javascript/apis/fetching-data/can-store/products.json'); + const response = await fetch( + "https://mdn.github.io/learning-area/javascript/apis/fetching-data/can-store/products.json", + ); if (!response.ok) { throw new Error(`HTTP error: ${response.status}`); } const data = await response.json(); return data; - } - catch (error) { + } catch (error) { console.error(`Could not get products: ${error}`); } } @@ -347,14 +378,15 @@ promise.then((data) => console.log(data[0].name)); ```js try { // await を非同期関数の外で使用することは、モジュールの中でしか許されません。 - const response = await fetch('https://mdn.github.io/learning-area/javascript/apis/fetching-data/can-store/products.json'); + const response = await fetch( + "https://mdn.github.io/learning-area/javascript/apis/fetching-data/can-store/products.json", + ); if (!response.ok) { throw new Error(`HTTP error: ${response.status}`); } const data = await response.json(); console.log(data[0].name); -} -catch(error) { +} catch (error) { console.error(`Could not get products: ${error}`); } ``` diff --git a/files/ja/learn/javascript/building_blocks/build_your_own_function/index.md b/files/ja/learn/javascript/building_blocks/build_your_own_function/index.md index 6359a0ad03e6fd..3fe1213997d22f 100644 --- a/files/ja/learn/javascript/building_blocks/build_your_own_function/index.md +++ b/files/ja/learn/javascript/building_blocks/build_your_own_function/index.md @@ -8,15 +8,15 @@ slug: Learn/JavaScript/Building_blocks/Build_your_own_function 前の記事で扱った重要な理屈をたくさん使って、この記事では実践的な練習を行ないます。ここではあなたが自力で独自関数を作成するための練習を行なっていきます。同時に、関数を扱う上で役に立つ細々の説明もしていきます。 | 前提知識: | 基本的なコンピューターの知識、HTML と CSS への理解、[JavaScript の第一歩](/ja/docs/Learn/JavaScript/First_steps)、[関数 — 再利用可能なコードブロック](/ja/docs/Learn/JavaScript/Building_blocks/Functions)。 | -| --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| 目的: | 独自の関数を作成する練習、役に立つ関連事項についてつっこんだ説明。 | +| --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| 目的: | 独自の関数を作成する練習、役に立つ関連事項についてつっこんだ説明。 | ## Active learning: 関数を作ってみよう これから作ってみる独自の関数を `displayMessage()`。これは独自のメッセージボックスをウェブページ上に表示し、ブラウザー組込みの [alert()](/ja/docs/Web/API/Window/alert) 関数の特製の代替品として動作します。既に見たものですが、忘れた事にしましょう。以下をブラウザーの JavaScript コンソールから打ち込みます、どのページでも構いません: ```js -alert('This is a message'); +alert("This is a message"); ``` `alert` 関数は引数を一つ取ります — アラートボックスに表示される文字列です。文字列を色々変えてメッセージを変化させてみて下さい。 @@ -34,42 +34,40 @@ alert('This is a message'); 1. [function-start.html](https://github.com/mdn/learning-area/blob/master/javascript/building-blocks/functions/function-start.html) ファイルにアクセスして、ローカルコピーを作成するところから初めます。HTML は単純です — body にはボタン一つしかありません。特製メッセージボックス用の基本的な CSS スタイルと、JavaScript を追加していく用の空の {{htmlelement("script")}} 要素が含まれています。 2. 次に、` @@ -339,14 +351,14 @@ for (let i = 0; i < divs.length; i++) {
- +
- +
- +

@@ -361,17 +373,17 @@ div { さあちょっとした JavaScript です — ここでは [onsubmit](/ja/docs/Web/API/GlobalEventHandlers/onsubmit) イベントハンドラー(フォームがサブミットされるとサブミットイベントが発火します)の中で、テキストフィールドが空かどうかテストするだけのとても簡単なチェックを実装します。もし空なら、イベントオブジェクトの [`preventDefault()`](/ja/docs/Web/API/Event/preventDefault) 関数— これでフォームの送信を抑制します — を呼び、それからフォームの下にあるパラグラフに、何が問題なのかユーザーに伝えるためのエラーメッセージを表示します: ```js -const form = document.querySelector('form'); -const fname = document.getElementById('fname'); -const lname = document.getElementById('lname'); -const para = document.querySelector('p'); +const form = document.querySelector("form"); +const fname = document.getElementById("fname"); +const lname = document.getElementById("lname"); +const para = document.querySelector("p"); -form.onsubmit = function(e) { - if (fname.value === '' || lname.value === '') { +form.onsubmit = function (e) { + if (fname.value === "" || lname.value === "") { e.preventDefault(); - para.textContent = 'You need to fill in both names!'; + para.textContent = "You need to fill in both names!"; } -} +}; ``` 言うまでもなく弱っちいフォームの検証です — 例えばフォームに空白や数字が入っていても止められません — が、例としては十分です。結果はこうなります。 @@ -385,21 +397,25 @@ form.onsubmit = function(e) { ここで最後に説明していくのは、滅多には遭遇しませんが、理解できていないととても苦痛になるかもしれない事柄です。ある一つの要素で同じイベントに紐付く二つのハンドラが活性化された時に何が起きるのかを説明するのが、イベントのバブリングとキャプチャリングという二種類のメカニズムです。わかりやすくするために次の例を見てください — [show-video-box.html](http://mdn.github.io/learning-area/javascript/building-blocks/events/show-video-box.html) 例を新しいタブで開いてください ([ソースコード](https://github.com/mdn/learning-area/blob/master/javascript/building-blocks/events/show-video-box.html) もまた別のタブに)。ライブでも下で見られます: ```html hidden - + - + Show video box example @@ -423,36 +438,41 @@ form.onsubmit = function(e) { @@ -467,9 +487,12 @@ form.onsubmit = function(e) { ``` @@ -477,9 +500,9 @@ form.onsubmit = function(e) { {{htmlelement("button")}} がクリックされると、`
` のクラス属性を `hidden` から `showing` に変更するので、ビデオが表示されます(例の CSS にこの二つのクラスが含まれており、それぞれはボックスの位置をスクリーンの外、内にします)。 ```js -btn.onclick = function() { - videoBox.setAttribute('class', 'showing'); -} +btn.onclick = function () { + videoBox.setAttribute("class", "showing"); +}; ``` では二つばかり `onclick` イベントハンドラーを追加します — 最初のは `
` に、二つ目は `