Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

Button HTML Tags นอก Form ใช้ได้หรือไม่?

<button> ใช้ได้ทุกที่ใน <body>

<button> เป็น Valid HTML ทุกที่ที่ Text-level markup อนุญาต (ไม่จำเป็นต้องอยู่ใน <form>)

ความแตกต่างกับ <input type="button">

  • <button> - ใส่เนื้อหาได้ (Text, Images, HTML elements)
  • <input type="button"> - ใส่เนื้อหาไม่ได้ ใช้แค่ value attribute

Default Type

นอก Form: type="button" (ไม่ Submit)
ใน Form: type="submit" (Submit Form) - ควรระบุ type="button" ชัดเจน

แนะนำ: สร้าง Button ด้วย JavaScript

หาก Button ต้องการ JS เท่านั้น ให้สร้างด้วย JS ป้องกันปุ่มทำงานไม่ได้เมื่อปิด JS