ITDS241 · บทที่ 2 · Lecture 02

Media, Forms & Semantic

HTML ส่วนที่ 2 — ใส่เสียงและวิดีโอด้วย HTML5, สร้าง form รับข้อมูลด้วย input หลายประเภท, ตรวจสอบข้อมูลด้วย validation attributes, ส่ง form ด้วย GET/POST และวางโครงหน้าเว็บด้วย semantic elements

เนื้อหาแยกตามหัวข้อในสไลด์

HTML5: Audio <audio>

HTML5 มี tag <audio> สำหรับใส่เสียงลงในหน้าเว็บ และรองรับไฟล์เสียง 3 รูปแบบ ได้แก่ MP3, WAV และ OGG พฤติกรรมของเครื่องเล่นกำหนดได้ด้วย attribute 2 ตัวหลัก

autoplay
เริ่มเล่นอัตโนมัติเมื่อเปิดหน้า
controls
เพิ่มปุ่มควบคุมเสียง (audio controls) ให้ผู้ใช้กดเล่น/หยุด/ปรับเสียงได้เอง

ไฟล์เสียงระบุด้วย <source> ที่อยู่ภายใน <audio> พร้อม src (ที่อยู่ไฟล์) และ type (ชนิดไฟล์) ส่วนข้อความที่อยู่ภายใน tag คือข้อความที่จะแสดงเมื่อ browser ไม่รองรับ tag audio

<audio autoplay controls>
    <source src="LetItGo.mp3" type="audio/mp3">
    Your browser does not support the audio tag.
</audio>

HTML5: Video <video>

ก่อนมี HTML5 วิดีโอจะเล่นใน browser ได้ก็ต่อเมื่อมี plug-in (เช่น Flash) เท่านั้น HTML5 จึงเพิ่ม tag <video> ที่รองรับไฟล์วิดีโอรูปแบบ MP4, WebM และ OGG โดยใช้ attribute เหมือนกับ audio คือ autoplay เพื่อเริ่มเล่นอัตโนมัติ และ controls เพื่อเพิ่มปุ่มควบคุมวิดีโอ เช่น play, pause และ volume

ตัวอย่างในสไลด์กำหนดขนาดด้วย width="320" และ height="240" และระบุ <source> ไว้ 2 ไฟล์ คือ small.mp4 (type video/mp4) และ small.ogg (type video/ogg)

<video width="320" height="240" autoplay controls>
    <source src="small.mp4" type="video/mp4">
    <source src="small.ogg" type="video/ogg">
    Your browser does not support the video tag.
</video>
ข้อควรระวัง: อย่าสลับรูปแบบไฟล์

Audio = MP3, WAV, OGG · Video = MP4, WebM, OGG — มีแค่ OGG ที่อยู่ทั้งสองกลุ่ม

HTML5: ฝังวิดีโอ YouTube

ถ้าวิดีโออยู่บน YouTube ไม่ต้องใช้ <video> แต่ให้ใช้ <iframe> ร่วมกับ YouTube Video Id เพื่ออ้างอิงวิดีโอในโค้ด HTML ในตัวอย่างนี้ Video Id คือ 62bIsvRcPv0 ซึ่งอยู่ท้าย URL https://www.youtube.com/embed/

<iframe width="320" height="240"
  src="https://www.youtube.com/embed/62bIsvRcPv0">
</iframe>

HTML Form คืออะไร

HTML form คือกลุ่มของชุดคำสั่งที่สร้างขึ้นเพื่อรับ input จากผู้ใช้ การสร้าง form เริ่มจาก tag <form>...</form> แล้วจึงใส่ form element ต่างๆ เช่น <input>, <button> ไว้ระหว่าง tag เปิดและปิดของ form

ตัวอย่าง form สมบูรณ์จากสไลด์ (Application Form) ประกอบด้วยกรอบ <fieldset> ที่มีหัวข้อ "Student Information" ช่องกรอก Student ID และ Name (type text) ช่องเลือกวันเกิด (type date) และปุ่ม Submit ทุก <label> ผูกกับช่องกรอกของตัวเองผ่าน for ที่ตรงกับ id

<form>
    <fieldset>
        <legend>Student Information</legend>
        <label for="txtStudentID">Student ID</label><br>
        <input type="text" id="txtStudentID"><br>
        <label for="txtName">Name</label><br>
        <input type="text" id="txtName"><br>
        <label for="cldBD">Birth Date</label><br>
        <input type="date" id="cldBD">
        <button type="submit">Submit</button>
    </fieldset>
</form>

Form Elements ภาพรวม

Tagคำอธิบาย
<input>ช่องที่ผู้ใช้กรอกข้อมูลได้ (input field)
<textarea>กล่องรับข้อความหลายบรรทัด (multi-line text input)
<select>สร้าง drop-down list ใช้คู่กับ <option>
<option>ตัวเลือกหนึ่งรายการใน select list
<button>ปุ่มที่คลิกได้ (type: submit, reset, button)
<fieldset>วาดกรอบจัดกลุ่ม element ที่เกี่ยวข้องกันใน form
ข้อสำคัญ (Note ในสไลด์)

All form elements must be within <form> tags — form element ทุกตัวต้องอยู่ภายใน <form> เท่านั้น

<input> เป็นชุดคำสั่งหลักสำหรับรับค่าใน form และระบุประเภทด้วย attribute type ประเภทที่เรียนในบทนี้คือ text, password, radio, checkbox, email, tel และ date (รวมถึงตัวแปรอื่นของวันเวลา เช่น month, week, datetime-local)

type="text" และ type="password"

type="text" สร้างช่องกรอกข้อความบรรทัดเดียว (one-line text input field) ใช้คู่กับ <label> ที่มี attribute for เพื่อกำหนดป้ายชื่อให้ form element โดยค่าของ for ต้องตรงกับ id ของช่องที่ต้องการผูก

<label for="txtFN">First name</label>
<input type="text" id="txtFN"><br>
<label for="txtLN">Last name</label>
<input type="text" id="txtLN">
ทำไมต้องผูก label ด้วย for/id

สไลด์ระบุว่า <label> กับ attribute for ที่อ้างถึง id มีประโยชน์มากสำหรับ screen readers — โปรแกรมอ่านหน้าจอจะรู้ว่าช่องกรอกนั้นชื่ออะไร

type="password" สร้างช่องกรอกบรรทัดเดียวที่ปิดบังตัวอักษร (masked) ใช้สำหรับรหัสผ่าน

<label for="txtLogin">Login</label>
<input type="text" id="txtLogin"><br>
<label for="txtPwd">Password</label>
<input type="password" id="txtPwd">

type="radio" และ type="checkbox"

type="radio" สร้างปุ่มตัวเลือกที่ใช้เมื่อเลือกได้เพียงตัวเลือกเดียว ตัวอย่างในสไลด์เป็นการเลือกกรุ๊ปเลือด 4 ตัวเลือก ทุกปุ่มมี name="bloodGr" เหมือนกัน แต่ value ต่างกัน และปุ่ม A มี checked จึงถูกเลือกไว้ตั้งแต่แรก

Blood Group: <br>
<input type="radio" name="bloodGr" value="A" checked>A<br>
<input type="radio" name="bloodGr" value="B">B<br>
<input type="radio" name="bloodGr" value="AB">AB<br>
<input type="radio" name="bloodGr" value="O">O<br>
ทำไม name ต้องเหมือนกัน

ค่าของ attribute name ต้องเหมือนกันเพื่อจัดกลุ่ม radio buttons — browser จึงรู้ว่าปุ่มเหล่านี้อยู่กลุ่มเดียวกันและให้เลือกได้ตัวเดียว ถ้า name ต่างกัน แต่ละปุ่มจะกลายเป็นคนละกลุ่ม

ใช้ checked เพื่อกำหนดปุ่มที่ถูกเลือกเป็นค่าเริ่มต้น

type="checkbox" สร้างกล่องเลือกที่ใช้เมื่อเลือกได้หลายตัวเลือกพร้อมกัน (multiple-choice) หลักการตั้ง name ให้เหมือนกันเพื่อจัดกลุ่ม และใช้ checked กำหนดกล่องที่ถูกเลือกไว้ตั้งแต่แรก (กี่กล่องก็ได้) เหมือนกับ radio

What should I have for lunch? <br>
<input type="checkbox" name="food" value="SW">Sandwich<br>
<input type="checkbox" name="food" value="FC">Fried Chicken<br>
<input type="checkbox" name="food" value="HB">Hamburger<br>
radio

เลือกได้ 1 ตัวเลือก ต่อกลุ่ม (กลุ่ม = name เดียวกัน)

checkbox

เลือกได้หลายตัวเลือกพร้อมกัน

type="tel", type="email" และ type="date"

type="tel" สร้างช่องสำหรับเบอร์โทรศัพท์ ซึ่งจะถูกตรวจสอบว่าตรงกับรูปแบบ (pattern) ที่กำหนดเมื่อ submit form รูปแบบนั้นเขียนเป็น regular expression ใน attribute pattern ตัวอย่างนี้บังคับรูปแบบ XXX-XXX-XXXX

What is your mobile phone?
<input type="tel" name="mobile" pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}">
<!-- Pattern: XXX-XXX-XXXX -->
<button type="submit">Submit</button>

type="email" สร้างช่องสำหรับอีเมล ซึ่งจะถูกตรวจสอบว่าเป็นรูปแบบของอีเมลเมื่อ submit form โดยไม่ต้องเขียน pattern เอง

What is your email?
<input type="email" name="userEmail">
<button type="submit">Submit</button>

type="date" แสดง date picker ให้ผู้ใช้เลือกวันที่ และจำกัดช่วงวันที่ได้ด้วย attribute min และ max ในตัวอย่างนี้ max="2010-01-01" หมายความว่าเลือกวันที่หลังจาก 1 มกราคม 2010 ไม่ได้

When is your birthday?
<input type="date" name="bd_d" max="2010-01-01">

นอกจาก date ยังมีตัวแปรอื่นที่เกี่ยวกับวันและเวลาอีก ได้แก่ type="week", type="month", type="datetime-local" และ type="time"

<input type="week"><br>
<input type="month"><br>
<input type="datetime-local"><br>
<input type="time">
ข้อควรระวัง: min / max

max = ค่าสูงสุด → ห้ามเลือกหลังวันนั้น · min = ค่าต่ำสุด → ห้ามเลือกก่อนวันนั้น — max="2010-01-01" ไม่ได้บังคับให้เลือกวันนั้นพอดี

attribute pattern และ Regular Expression

attribute pattern กำหนด regular expression ที่ค่าของ <input> จะถูกนำไปตรวจสอบเมื่อ submit form ส่วน regular expression คือลำดับของตัวอักษรที่ประกอบกันเป็นรูปแบบสำหรับค้นหา (search pattern) ตัวอย่างในสไลด์มีดังนี้

Regular Expressionความหมายตัวอย่างค่าที่ผ่าน
[A-Za-z]{3}ตัวอักษรภาษาอังกฤษ 3 ตัวเท่านั้นTHA · tha · Tha · tHA
[A-Z]{5,}ตัวพิมพ์ใหญ่ 5 ตัวขึ้นไปHELLO · WORLD
.{2,4}ตัวอักษรใดก็ได้ 2 ถึง 4 ตัว$o · Wh@+ · T0o
[0-9]{2}88[0-9]{3}ตัวเลขรูปแบบ ##88###5088111 · 6288999
ลองเล่น: ทดสอบ pattern จากสไลด์ (ตรวจด้วย browser จริง)

ตัวอย่างค่าที่ผ่านจากสไลด์: THA · tha · Tha · tHA

<textarea> และ <select>

<textarea> สร้างช่องรับข้อความหลายบรรทัด (multi-line text input control) ขนาดของกล่องกำหนดได้ด้วย attribute cols (จำนวนคอลัมน์) และ rows (จำนวนแถว) ข้อความที่อยู่ระหว่าง tag เปิดและปิดจะแสดงอยู่ในกล่องตั้งแต่แรก

Address:<br>
<textarea rows="3" cols="20">Input your
Thai Address here</textarea>

<select> สร้าง drop-down list โดยใส่ <option> ไว้ภายในเพื่อกำหนดตัวเลือกแต่ละรายการ ใช้ attribute selected กับ option ที่ต้องการให้เป็นค่าเริ่มต้น

Major:
<select name="majors">
    <option value="CS">Comp.Sci.</option>
    <option value="CE">Comp.Eng.</option>
    <option value="IS">Info.Sci.</option>
    <option value="IT">Info.Tech.</option>
</select>

<button> และ <fieldset>

<button> กำหนดปุ่มที่คลิกได้ ซึ่งภายในใส่ได้ทั้งข้อความและรูปภาพ ปุ่มมี 3 ประเภท ตาม attribute type

type="button"
ปุ่มทั่วไป (general-purpose) เช่น คลิกเพื่อเรียกใช้ฟังก์ชัน
type="reset"
ปุ่มสำหรับตั้งค่า form กลับสู่สถานะเริ่มต้น
type="submit"
ปุ่มสำหรับส่ง input ทั้งหมดใน form ไปยังปลายทาง

ตัวอย่างในสไลด์มีทั้งปุ่ม submit และปุ่ม reset (ข้อความ Clear) ภายใน form ที่ส่งไปยัง somewhere.html ด้วย method GET (รายละเอียดเรื่อง action/method อยู่ในการ์ดถัดไป)

<form action="somewhere.html" method="GET">
    First name:<input type="text" name="fistname"><br>
    Last name:<input type="text" name="lastname"><br>
    <button type="submit">Submit</button>
    <button type="reset">Clear</button>
</form>

<fieldset> วาดกรอบรอบกลุ่ม element ที่เกี่ยวข้องกันใน form พร้อมหัวข้อกำกับ (caption) ซึ่งกำหนดด้วย <legend>

<fieldset>
    <legend>Name</legend>
    First name:<br>
    <input type="text" name="fistname"><br>
    Last name:<br>
    <input type="text" name="lastname"><br>
</fieldset>
แบบฝึกหัดในสไลด์: [EX2] Sign-Up Form

จงสร้าง Sign-up Form ที่ประกอบด้วย input box สำหรับชื่อนักศึกษา · input box สำหรับอีเมล · input box ลักษณะปฏิทินที่วันที่สิ้นสุดคือ 1 มกราคม 2565 · ปุ่ม submit · ปุ่ม reset

Form Validation

เมื่อผู้ใช้กรอกข้อมูล browser จะตรวจสอบว่าข้อมูลอยู่ในรูปแบบที่ถูกต้อง และอยู่ภายในเงื่อนไข (constraints) ที่กำหนด การตรวจสอบ form ฝั่ง client ทำได้ 2 วิธี คือ

HTML5

ใช้ form attributes (หัวข้อถัดไป)

JavaScript

เขียนโค้ดตรวจสอบเอง

HTML5 Form Attributes (Restriction attributes)

attribute กลุ่มนี้คือเงื่อนไขที่ใช้ในการ validate form

Attributeคำอธิบาย
minกำหนดค่าต่ำสุดของ <input>
maxกำหนดค่าสูงสุดของ <input>
patternกำหนด regular expression ที่ค่าของ <input> ต้องตรงตาม
titleอธิบาย pattern เพื่อช่วยผู้ใช้ (ใช้คู่กับ pattern)
placeholderข้อความ hint ที่อธิบายค่าที่คาดหวังในช่องกรอก
requiredบังคับให้ต้องกรอกช่องนี้ก่อนจึงจะ submit form ได้
ข้อควรระวัง: placeholder ≠ required

placeholder เป็นแค่ข้อความ hint ในช่อง ไม่ได้บังคับให้กรอก · required คือตัวที่บังคับให้กรอกก่อน submit · title อธิบาย pattern ให้ผู้ใช้เข้าใจ

ตัวอย่างจากสไลด์ใช้ครบหลายตัว: ช่อง Mobile มี pattern และ title อธิบายรูปแบบ, ช่อง E-mail มี placeholder, required และ title, ช่อง Country code มี pattern="[A-Z]{3}" พร้อม title

<form>
    First name:<br><input type="text" name="fname"><br>
    Last name:<br><input type="text" name="lname"><br>
    Mobile:<br>
    <input type="tel" pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}"
           title="Mobile: XXX-XXX-XXXX"><br>
    E-mail:<br>
    <input type="email" name="userid" placeholder="Your e-mail" required
           title="Required e-mail"><br>
    Country code:<br>
    <input type="text" pattern="[A-Z]{3}" title="A country code with three alphabet"><br>
    <p><button type="submit">Submit</button></p>
</form>
ลองเล่น: form ตัวอย่างข้างบนแบบกดส่งได้จริง (browser ตรวจ validation จริง แต่ไม่ส่งข้อมูลไปไหน)

ลองพิมพ์ 0812345678 (ไม่มีขีด) แล้วกด Submit

ลองเว้นว่าง หรือพิมพ์คำที่ไม่มี @

ลองพิมพ์ tha (ตัวพิมพ์เล็ก)

Form Submission

เมื่อผู้ใช้คลิกปุ่ม submit ข้อมูลทั้งหมดใน form จะถูกส่งออกไปยังหน้าปลายทางหรือ web server เพื่อประมวลผล การส่งถูกกำหนดด้วย attribute 2 ตัวของ <form>

action
กำหนดว่าจะส่ง form-data ไปที่ไหน เมื่อ submit
method
กำหนด HTTP method (GET/POST) ที่ใช้ส่ง form-data

Form ทำงานอย่างไร (How the Form works)

  1. ผู้ใช้กรอกข้อมูลแล้วกดปุ่ม Submit
  2. browser ทำ Form Validation
  3. ถ้า form ไม่ผ่าน (invalid) จะย้อนกลับไปที่ form ให้ผู้ใช้แก้ไข ข้อมูลยังไม่ถูกส่ง
  4. ถ้าผ่าน validation จะไปยัง server หรือปลายทางที่กำหนดใน action ด้วย HTTP method GET หรือ POST
ขั้นตอนการทำงานของ form กด Submit แล้วเข้าสู่ Form Validation ถ้าไม่ผ่านย้อนกลับไปที่ form ถ้าผ่านไปยัง server หรือปลายทางใน action ด้วย GET ซึ่งส่งข้อมูลผ่าน URL หรือ POST ซึ่งส่งข้อมูลภายใน packet <fieldset> Student Information First name Harry Last name Potter Submit Form Validation If the form is invalid ย้อนกลับไปที่ form · ไม่ส่งข้อมูล Form passed validation HTTP: GET/POST Go to Server or Destination defined in action GET ข้อมูลอยู่ใน URL เป็น name=value POST ข้อมูลอยู่ภายใน packet
วาดใหม่จากสไลด์ 42 "How the Form works" รวมกับสไลด์ 41 เรื่อง GET/POST — รายละเอียดฝั่ง server จะได้เรียนตอนทำ form handling

GET vs POST

GET

ส่งข้อมูลทั้งหมดผ่าน URL — ข้อมูลที่ได้รับจะแสดงอยู่ใน url path เป็นคู่ของ name และ value ซึ่ง name ตรงกับชื่อ (attribute name) ของช่องใน form ที่ส่งมา

POST

ส่งข้อมูลทั้งหมดภายใน packet — ข้อมูลที่ได้รับจะถูกบรรจุไว้ใน packet เพื่อนำไปใช้ ไม่ได้แสดงใน URL

ตัวอย่างทั้งสองแบบส่งข้อมูลไปยัง somewhere.html เหมือนกัน ต่างกันแค่ค่า method (ชื่อช่อง fistname สะกดตามสไลด์)

<form action="somewhere.html" method="GET">
    First name:<input type="text" name="fistname"><br>
    Last name:<input type="text" name="lastname"><br>
    <button type="submit">Submit</button>
</form>
<form action="somewhere.html" method="POST">
    First name:<input type="text" name="fistname"><br>
    Last name:<input type="text" name="lastname"><br>
    <button type="submit">Submit</button>
</form>
ข้อควรระวัง: action vs method

action = ส่งไปที่ไหน · method = ส่งด้วยวิธีไหน (GET/POST) — โจทย์มักถามสลับกัน

Semantic Elements คืออะไร

คำว่า Semantic แปลว่า "มีความหมาย" (related to meaning in language or logic) semantic elements คือ tag ที่ใช้กำหนดส่วนต่างๆ ของหน้าเว็บ หรือใช้วางโครงสร้างของหน้าเว็บ (layout) ซึ่งจะนำไปใช้ร่วมกับ CSS ต่อไป

ทำไมต้องใช้ semantic elements

เมื่อหน้าเว็บมีข้อมูลเชิงความหมายมากขึ้น (more semantic information) screen readers หรือ search engines จะระบุได้ว่าเนื้อหาแต่ละส่วนของหน้าเว็บคืออะไร

ก่อนมี "Semantic" Elements

ก่อนหน้านี้ นักพัฒนาใช้ tag ทั่วไปอย่าง <div> ร่วมกับ attribute class เพื่อแบ่งเนื้อหาออกเป็นส่วนๆ เช่น header, menubar, section, article และ footer ชื่อของส่วนเหล่านี้อยู่แค่ในค่าของ class ส่วนตัว tag ทุกตัวยังเป็น <div> เหมือนกันหมด

<body>
    <div class="header">Header</div>
    <div class="menubar">Menu Bar/Navigation Bar</div>
    <div class="section">
        <div class="article">Content Article 1</div>
        <div class="article">Content Article 2</div>
    </div>
    <div class="footer">Document Footer</div>
</body>

HTML5 Semantic Elements

Tagคำอธิบาย
<header>ส่วนหัวของเอกสาร หรือของ section
<nav>ลิงก์นำทาง (navigation links) หรือเมนูบาร์
<section>section หนึ่งในเอกสาร
<article>article หนึ่งในเอกสาร
<aside>เนื้อหาที่แยกออกมาจากเนื้อหาหลัก (aside from the content)
<footer>ส่วนท้ายของเอกสาร หรือของ section

โครงหน้าเว็บตัวอย่างในสไลด์วาง element ทั้ง 6 ตัวเรียงกันใน <body> พร้อม comment บอกหน้าที่ของแต่ละส่วน แผนผังด้านล่างแสดงตำแหน่งที่มักพบในหน้าเว็บทั่วไป

<!DOCTYPE html>
<html>
<head>
    <title>HTML5 Semantic Elements</title>
</head>
<body>
    <header><!--Document Header--></header>
    <nav><!-- Navigation tabs / Menu bars --></nav>
    <section><!-- Doc. section 1 --></section>
    <article><!-- Doc. section 2 --></article>
    <aside><!-- Sidebar --></aside>
    <footer><!-- Footer--></footer>
</body>
</html>

ตัวอย่างการใช้ semantic element แต่ละตัว

<header> — ในตัวอย่างใส่หัวข้อหลักของหน้าไว้ในส่วนหัว

<header><!-- Document Header -->
    <h1>This is Header Part</h1>
</header>

<footer> — ในตัวอย่างใส่ข้อมูลผู้โพสต์และข้อมูลติดต่อไว้ในส่วนท้าย

<footer><!-- Footer -->
    <p>Posted by: J.K.</p>
    <p>Contact information: jidapa.kra[AT]mahidol.edu</p>
</footer>

<nav> — รวมลิงก์นำทางไปยังหัวข้อต่างๆ ไว้ด้วยกัน

<nav><!-- Navigation tabs/Menu bars -->
    <a href="/html/">HTML</a> |
    <a href="/html5/">HTML5</a> |
    <a href="/css/">CSS</a> |
    <a href="/js/">JavaScript</a>
</nav>

<section> — ในตัวอย่างเป็นส่วนเนื้อเพลงที่มีหัวข้อ <h2> และย่อหน้า

<section><!-- Doc. section 1 -->
    <h2>Lyrics</h2>
    <p>Here's to the ones that we got<br>
       Cheers to the wish you were here,...
    </p>
</section>

<article> — ตัวอย่าง "Famous Cities" เป็น article ใหญ่ที่มี article เล็ก (One small article) ซ้อนอยู่ข้างใน 3 ชิ้น แต่ละชิ้นพูดถึงเมืองหนึ่งเมืองได้สมบูรณ์ในตัวเอง

<article>
    <h2>Famous Cities</h2>
    <article>
        <h2>London</h2>
        <p>London is the capital city of England.</p>
    </article>
    <article>
        <h2>Paris</h2>
        <p>Paris is the capital and most populous city of France.</p>
    </article>
    <article>
        <h2>Tokyo</h2>
        <p>Tokyo is the capital of Japan.</p>
    </article>
</article>

<aside> — ข้อความในตัวอย่างสไลด์บอกหลักการไว้เองว่า "This content should be related to the surrounding content." คือเนื้อหาข้างเคียงควรเกี่ยวข้องกับเนื้อหารอบๆ แต่แยกออกมาเป็นส่วนเสริม เช่น sidebar

<aside><!-- Sidebar -->
    <h2>
        This content should be related<br>
        to the surrounding content.
    </h2>
</aside>

<article> หรือ <section>?

<article>

เนื้อหาที่เป็นอิสระและสมบูรณ์ในตัวเอง (independent, self-contained) เช่น โพสต์ในฟอรัม บล็อกโพสต์ news feeds หรือคอมเมนต์

<section>

section ทั่วไปของเอกสารหรือแอปพลิเคชัน (a generic document or application section)

Note ในสไลด์

<article> อยู่ภายใน <section> ได้ และ <section> ก็มี <article> อยู่ข้างในได้ — ซ้อนกันได้ทั้งสองทิศทาง

Web Layouts

รูปแบบการจัดวางหน้าเว็บมีอยู่มากมาย สไลด์ยกตัวอย่างโครงพื้นฐานอย่างง่ายไว้ ประกอบด้วย Header บนสุด ตามด้วย Navigation Bar จากนั้นเป็นส่วนเนื้อหาที่แบ่งเป็น Content (ซ้าย) · Main Content (กลาง) · Content (ขวา) และปิดท้ายด้วย Footer

นอกจากนี้ layout ของเนื้อหาควรเปลี่ยนไปตามอุปกรณ์ (the Web Content Layout should vary by the devices) ลองกดสลับดูแต่ละแบบ

Navigation Bar
Content
Main Content
Content

Desktop Browser · 3-column — Content ซ้าย, Main Content กลาง, Content ขวา

ตัวอย่าง HTML Layout

ตัวอย่างในสไลด์สร้าง layout 3 คอลัมน์ด้วย semantic elements: <header> · <nav> ที่มีลิงก์ 3 ลิงก์ · <article class="row"> ที่ครอบ <article class="column"> 3 ชิ้น (Column1–3) · และ <footer>

<header>
    <h1>Header</h1>
</header>
<nav>
    <a href="#">Link1</a>
    <a href="#">Link2</a>
    <a href="#">Link3</a>
</nav>
<article class="row">
    <article class="column">
        <h2>Column1</h2>
        <p>Lorem ipsum dolor sit amet … </p>
    </article>
    <article class="column">
        <h2>Column2</h2>
        <p>Lorem ipsum dolor sit amet … </p>
    </article>
    <article class="column">
        <h2>Column3</h2>
        <p>Lorem ipsum dolor sit amet … </p>
    </article>
</article>
<footer>
    <h1>Footer</h1>
</footer>
ข้อควรระวัง: ไม่มี CSS = ไม่มีคอลัมน์

สไลด์ "HTML Layout (No CSS)" แสดงว่าถ้ายังไม่ใส่ CSS ทุกส่วนจะเรียงต่อกันในแนวตั้ง (Header → Navigation Bar → Column1 → Column2 → Column3 → Footer) ดังผลลัพธ์ด้านบน semantic elements บอกแค่ความหมายของแต่ละส่วน การจัดให้เป็นคอลัมน์ต้องใช้ CSS ซึ่งจะเรียนละเอียดในสัปดาห์ถัดไป

สไลด์ "HTML Layout (with CSS)" ยกตัวอย่างส่วนหนึ่งของไฟล์ CSS ที่ใช้ตกแต่ง header, footer และ nav

header, footer {
    background-color: #f1f1f1;
    padding: 3px;
    text-align: center;
}
/* Navigation bar */
nav {
    overflow: hidden;
    background-color: darkblue;
}
...

Cheat sheet

media, input type, validation, GET/POST และ semantic elements ของบทที่ 2 รวมไว้ในหน้าเดียว

01 Audio · Video · YouTube

<audio><video>
รูปแบบไฟล์MP3, WAV, OGGMP4, WebM, OGG
autoplayเล่นอัตโนมัติเล่นอัตโนมัติ
controlsปุ่มควบคุมเสียงปุ่ม play, pause, volume
ไฟล์<source src type="audio/mp3"><source> ได้หลายไฟล์ · width/height
อื่นๆข้อความใน tag แสดงเมื่อไม่รองรับก่อน HTML5 ต้องใช้ plug-in (Flash)

YouTube → <iframe src="https://www.youtube.com/embed/VIDEO_ID">

02 GET vs POST

GET
ข้อมูลใน URL เป็นคู่ name=value
POST
ข้อมูลใน packet ไม่แสดงใน URL
action
ส่งไปที่ไหน
method
ส่งด้วยวิธีไหน (GET/POST)

Submit → Validation → ไม่ผ่าน: กลับไปแก้ · ผ่าน: ไปปลายทางใน action

03 <input type> และ form elements

type / tagใช้ทำอะไรจุดจำ
textข้อความบรรทัดเดียว<label for> = id ของ input (screen reader)
passwordข้อความปิดบัง (masked)
radioเลือกได้ 1 ตัวต่อกลุ่มname เดียวกัน = กลุ่มเดียวกัน · checked = ค่าเริ่มต้น
checkboxเลือกได้หลายตัวname เดียวกัน · checked ได้หลายตัว
telเบอร์โทรตรวจตาม pattern ตอน submit
emailอีเมลตรวจรูปแบบอีเมลตอน submit
datedate pickermin/max · week, month, time, datetime-local
<textarea>ข้อความหลายบรรทัดrows, cols
<select>drop-down<option> · selected = ค่าเริ่มต้น
<button>ปุ่มbutton · reset (คืนค่าเดิม) · submit (ส่ง)
<fieldset>กรอบจัดกลุ่ม<legend> = หัวข้อกรอบ

form element ทุกตัวต้องอยู่ภายใน <form>

04 Validation attributes

min / max
ค่าต่ำสุด / สูงสุด (max = ห้ามเกินหรือหลังค่านั้น)
pattern
regular expression ที่ค่าต้องตรง
title
อธิบาย pattern ให้ผู้ใช้
placeholder
ข้อความ hint (ไม่บังคับ)
required
บังคับกรอกก่อน submit

validate ได้ 2 ทาง: HTML5 attributes หรือ JavaScript

05 Semantic elements

Tagความหมายตำแหน่งทั่วไป
<header>ส่วนหัวของเอกสาร/sectionบนสุด
<nav>ลิงก์นำทาง / เมนูบาร์ใต้ header
<section>section ทั่วไปของเอกสารเนื้อหาหลัก
<article>เนื้อหาสมบูรณ์ในตัวเอง (โพสต์, ข่าว, คอมเมนต์)เนื้อหาหลัก
<aside>เนื้อหาข้างเคียงที่เกี่ยวข้อง (sidebar)ด้านข้าง
<footer>ส่วนท้ายของเอกสาร/sectionล่างสุด

ก่อน HTML5 ใช้ <div class="..."> · ประโยชน์: screen reader / search engine เข้าใจเนื้อหา · Mobile 1-column · Tablet/Laptop 2-column · Desktop 3-column

06 article vs section

<article>
independent, self-contained — forum post, blog post, news feed, comment
<section>
generic section ของเอกสาร/แอป

ซ้อนกันได้ทั้งสองทิศทาง: article ใน section และ section ใน article

แบบฝึกหัด 20 ข้อ

เลือกคำตอบให้ครบแล้วกด "ตรวจคำตอบ" ด้านล่าง ระบบจะเฉลยพร้อมคำอธิบายทุกข้อ

01HTML5 รองรับไฟล์เสียง (audio) รูปแบบใดบ้าง?

Audio รองรับ 3 รูปแบบ: MP3, WAV และ OGG · ข้อ A (MP4, WebM, OGG) คือรูปแบบของ video

02attribute ใดใช้เพิ่มปุ่มควบคุม play/pause/volume ให้ <audio>/<video>?

controls เพิ่มปุ่มควบคุม เช่น play, pause และ volume · autoplay ทำให้เริ่มเล่นอัตโนมัติ

03การฝังวิดีโอ YouTube ในหน้า HTML ใช้ tag ใด?

ใช้ <iframe> ร่วมกับ YouTube Video Id เช่น src="https://www.youtube.com/embed/62bIsvRcPv0" · <video> ใช้กับไฟล์วิดีโอ MP4/WebM/OGG

04form element ทุกตัวต้องอยู่ภายใน tag ใด?

"All form elements must be within <form> tags" · <fieldset> เป็นแค่กรอบจัดกลุ่มที่อยู่ใน form อีกที

05<input type="radio"> กับ <input type="checkbox"> ต่างกันอย่างไร?

radio ใช้เมื่อเลือกได้เพียงตัวเลือกเดียว · checkbox ใช้เมื่อเลือกได้หลายตัวเลือก (multiple-choice) · ข้อ A สลับกัน

06ทำไม radio button หลายปุ่มในกลุ่มเดียวกันต้องใช้ attribute name ค่าเดียวกัน?

"The value of attribute name must be the same to group the radio buttons" — name เดียวกันคือการจัดกลุ่ม ทำให้เลือกได้ตัวเดียวในกลุ่ม

07attribute pattern ของ <input> ใช้ทำอะไร?

pattern กำหนด regular expression ที่ค่าของ <input> จะถูกตรวจสอบเมื่อ submit form เช่น [0-9]{3}-[0-9]{3}-[0-9]{4}

08<input type="date" max="2010-01-01"> มีผลอย่างไร?

max กำหนดค่าสูงสุด จึงเลือกวันที่หลัง 2010-01-01 ไม่ได้ · ข้อ C คือผลของ min

09attribute ใดบังคับให้ผู้ใช้ต้องกรอกข้อมูลในช่องก่อน submit form ได้?

required บังคับให้กรอกช่องนั้นก่อน submit · placeholder เป็นแค่ hint · title อธิบาย pattern · pattern กำหนดรูปแบบของค่า

10attribute placeholder มีหน้าที่อะไร?

placeholder "Provides a hint that describes the expected value of an input field" · การบังคับกรอกคือ required · การตรวจรูปแบบอีเมลมาจาก type="email"

11เมื่อ form ใช้ method="GET" ข้อมูลที่ส่งจะปรากฏที่ใด?

GET ส่งข้อมูลทั้งหมดผ่าน URL — ข้อมูลแสดงใน url path เป็นคู่ name และ value · ข้อ A คือ POST

12เมื่อ form ใช้ method="POST" ข้อมูลที่ส่งจะเป็นอย่างไร?

POST ส่งข้อมูลทั้งหมดภายใน packet ("all data received will be packed in the packet") ไม่ได้แสดงใน URL

13attribute ใดใน <form> กำหนดว่าจะส่งข้อมูลไปที่หน้าใด?

action กำหนดว่าจะส่ง form-data ไปที่ไหน (เช่น action="somewhere.html") · method กำหนด HTTP method (GET/POST)

14Semantic elements มีประโยชน์หลักคืออะไร?

"With more semantic information, screen readers or search engines can identify the contents of the web pages"

15<article> เหมาะกับเนื้อหาประเภทใดมากที่สุด?

<article> = independent, self-contained content เช่น forum post, blog post, news feeds, comments · เมนูนำทางคือ <nav> · ส่วนท้ายคือ <footer>

16ก่อนมี semantic elements นักพัฒนามักใช้ tag ใดร่วมกับ attribute class เพื่อแบ่งส่วนเนื้อหา?

"Use a general tag such <div> with the attribute class to partition the content" เช่น <div class="header">

17<aside> ใช้สำหรับเนื้อหาประเภทใด?

<aside> คือ content aside from the content (Sidebar) และ "This content should be related to the surrounding content."

18Layout แบบ 1-column มักใช้กับอุปกรณ์ประเภทใด?

Mobile Browser = 1-column · Tablet/Laptop Browser = 2-column · Desktop Browser = 3-column

19<article> และ <section> สามารถซ้อนกันได้หรือไม่?

"<article> can be inside <section> and <section> can also have <article> inside"

20tag ใดที่ควรอยู่ตำแหน่งบนสุดของโครงสร้างหน้าเว็บโดยทั่วไป?

โครงพื้นฐาน: Header (บนสุด) → Navigation Bar → เนื้อหา → Footer (ล่างสุด) · <header> คือส่วนหัวของเอกสาร

0 / 20 ข้อ

แบบฝึกหัดเขียนโค้ด

เขียน HTML จริงในช่องแก้ไข ดูผลลัพธ์ใน browser ทันที แล้วกด "ตรวจคำตอบ" ให้ระบบตรวจโครงสร้างให้อัตโนมัติ (Ctrl + Enter ก็ตรวจได้) · โค้ดและความคืบหน้าบันทึกไว้ใน browser นี้

แบบฝึกหัดเขียนโค้ดต้องเปิด JavaScript ใน browser