การทำงานร่วมกันด้วย JavaScript
แนะนำใหม่| English | ความหมาย |
|---|---|
| DOM/dɒm/ | โมเดลเอกสารวัตถุ |
| event/ɪˈvent/ | เหตุการณ์ |
| function/ˈfʌŋkʃn/ | ฟังก์ชัน |
A menu opens, but its button still says it is closed
- A visible panel and its accessibility state can disagree. A reliable interaction updates both the content and the control that describes it.
- Browser JavaScript responds to user actions. Do not put private credentials in client code; visitors can inspect and change what runs on their machines.
Find the parsed elements
- The DOM 文档对象模型 represents the document as nodes as HTML is parsed. Code can query an existing node and change its properties.
- A script running before its target is parsed may get
null. A deferred external script runs after parsing; a script at the end of the body can also work for elements already above it.
DOM แทนอะไร?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
โหนดจะพร้อมใช้งานเมื่อเอกสารถูก parse; การ query จะไม่พบเป้าหมายที่ยังไม่ได้ถูก parse
Register the response once
- An event 事件 records an action such as a click or form submission.
addEventListenerregisters a function 函数 to run when that event is dispatched. - Find the button and panel, register the listener, then update state on each click. Do not call the function while registering it if you mean to pass it as a handler.

วิธีใดที่ลงทะเบียน event handler กับองค์ประกอบ?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
addEventListener ลงทะเบียน callback สำหรับเหตุการณ์เช่น click
เรียงลำดับขั้นตอนสำหรับการตั้งค่าการโต้ตอบนี้
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
การลงทะเบียนprecedes การตอบสนองต่อเหตุการณ์; handler จะทำงานเมื่อมีการส่งเหตุการณ์
Keep a usable fallback
- Save this as an HTML file. The links are initially visible; the toggle button appears only after its handler is ready.
- Click the button twice. Check that the panel and
aria-expandedchange together. The native button also responds to keyboard activation.
<!doctype html>
<html lang="en">
<head><meta charset="utf-8"><title>Club menu</title></head>
<body>
<button id="menu-button" type="button" hidden
aria-controls="menu-panel" aria-expanded="true">Menu</button>
<nav id="menu-panel" aria-label="Club">
<a href="#meeting">Meeting details</a>
</nav>
<main id="meeting"><h1>Meet Thursday in room 12</h1></main>
<script>
const button = document.querySelector('#menu-button');
const panel = document.querySelector('#menu-panel');
button.addEventListener('click', () => {
panel.hidden = !panel.hidden;
button.setAttribute('aria-expanded', String(!panel.hidden));
});
button.hidden = false;
</script>
</body>
</html>
Initially: links visible, expanded true. First click: links hidden, expanded false. Second click: links visible, expanded true. With JavaScript disabled, the original links remain visible.
ในตัวอย่างที่มีคำตอบ JavaScript ถูกปิดใช้งาน สิ่งที่จะเกิดขึ้นกับลิงก์นำทางเดิม?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
ลิงก์เริ่มมองเห็นได้ ปุ่มเริ่มซ่อนอยู่ ดังนั้นการเพิ่มประสิทธิภาพที่ไม่สำเร็จจึงไม่ลบลบการนำทางที่สำคัญออก
จับคู่สถานะพาเนลแต่ละตัวกับสถานะปุ่มที่ถูกต้อง
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
aria-expanded อธิบายว่าพื้นที่ควบคุมกำลังขยายอยู่หรือไม่
Separate helpful checks from trusted checks
- Browser validation can give quick feedback, but the server must validate submitted values and permissions independently. A caller can bypass or alter browser code.
- Use
textContentto insert plain text rather than interpreting it as HTML. Check missing elements and errors before adding more features.
A click handler alone does not make a custom element keyboard accessible. Use the appropriate native control and verify focus and activation. Also check that your CSS does not override the panel's hidden state.
เซิร์ฟเวอร์ต้องตรวจสอบค่าที่ส่งเข้ามาเสมอ แม้ว่าจะมีการตรวจสอบโดยเบราว์เซอร์ไปแล้วก็ตาม
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
ถูกต้อง ผู้เรียกใช้งานสามารถข้ามหรือแก้ไขโค้ดในเบราว์เซอร์ได้ ดังนั้นเซิร์ฟเวอร์จึงต้องตรวจสอบค่าและสิทธิ์อย่างอิสระ
Trace two actions and a failure
- Record initial state and the state after two activations. Repeat using the keyboard, then check that essential navigation remains available without the script.
- Explain what runs in the browser and what still needs server checks. Test this local example without sending any real form data.
Parsed element → registered event handler → consistent state update. Preserve useful fallback content and keep trusted validation on the server.
แผงควบคุมเริ่มแสดงอยู่ การสลับสถานะทำงานสองครั้ง สถานะสุดท้ายคืออะไร?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
การคลิกครั้งแรกซ่อนมัน; การคลิกครั้งที่สองคืนค่ามันมา ทั้งสองสถานะต้องมีความสอดคล้องกัน
ข้อใดบ้างที่ควรอยู่ในการทบทวนปฏิสัมพันธ์เฉพาะนี้? เลือกทุกข้อที่ใช้ได้
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
ทดสอบการทำงาน สถานะ และทางเลือกสำรอง โค้ดเบราว์เซอร์ไม่ใช่สถานที่ปลอดภัยสำหรับข้อมูลรับรองความลับ