Bootstrap 4 บทช่วยสอน

BS4 HOME BS4 เริ่มต้น BS4 คอนเทนเนอร์ BS4 Grid Basic BS4 วิชาการพิมพ์ BS4 สี ตาราง BS4 BS4 รูปภาพ BS4 Jumbotron การแจ้งเตือน BS4 ปุ่ม BS4 กลุ่มปุ่ม BS4 ป้าย BS4 แถบความคืบหน้า BS4 BS4 Spinners BS4 การแบ่งหน้า BS4 รายการกลุ่ม การ์ด BS4 BS4 ดรอปดาวน์ BS4 ยุบ BS4 Navs BS4 Navbar แบบฟอร์ม BS4 อินพุต BS4 กลุ่มอินพุต BS4 BS4 แบบฟอร์มที่กำหนดเอง BS4 ม้าหมุน BS4 โมดอล เคล็ดลับเครื่องมือ BS4 BS4 ป๊อปโอเวอร์ BS4 ขนมปังปิ้ง BS4 Scrollspy BS4 ยูทิลิตี้ BS4 Flex ไอคอน BS4 วัตถุสื่อ BS4 ตัวกรอง BS4

Bootstrap 4 Grid

ระบบกริด BS4 BS4 ซ้อน/แนวนอน BS4 Grid XSmall BS4 กริดขนาดเล็ก BS4 กริดขนาดกลาง BS4 กริดขนาดใหญ่ BS4 Grid XLarge ตัวอย่างกริด BS4

Bootstrap 4 อื่นๆ

เทมเพลตพื้นฐาน BS4 แบบฝึกหัด BS4 แบบทดสอบ BS4

Bootstrap 4 อ้างอิง

ทุกชั้นเรียน JS Alert ปุ่ม JS JS Carousel JS ยุบ JS Dropdown JS Modal JS Popover JS Scrollspy JS แท็บ JS Toasts JS Tooltip


Bootstrap JS Dropdown


คลาส CSS แบบเลื่อนลง

สำหรับบทแนะนำเกี่ยวกับ Dropdowns โปรดอ่านBootstrap Dropdowns Tutorialของเรา

Class Description Example
.dropdown Indicates a dropdown menu
.dropdown-item Style links inside the dropdown menu with proper padding etc
.dropdown-item-text Style text or text links inside the dropdown menu with proper padding etc
.dropdown-menu Builds the dropdown menu
.dropdown-menu-right Right-aligns a dropdown menu
.dropdown-header Adds a header inside the dropdown menu
.dropup Indicates a dropup menu
.disabled Disables an item in the dropdown menu
.active Styles the active element in a dropdown menu
.divider Separates items inside the dropdown menu with a horizontal line

ผ่าน data-* คุณสมบัติ

เพิ่มdata-toggle="dropdown"ในลิงก์หรือปุ่มเพื่อสลับเมนูแบบเลื่อนลง

ตัวอย่าง

<button type="button" class="dropdown-toggle" data-toggle="dropdown">Dropdown Example</button>

ผ่าน JavaScript

เปิดใช้งานด้วยตนเองด้วย:

ตัวอย่าง

$('.dropdown-toggle').dropdown();

หมายเหตุ:จำเป็นต้องใช้แอตทริบิวต์ data-toggle="dropdown" ไม่ว่าคุณจะเรียกใช้เมธอด dropdown() หรือไม่


ตัวเลือกแบบเลื่อนลง

None

วิธีการแบบเลื่อนลง

ตารางต่อไปนี้แสดงวิธีการดรอปดาวน์ที่มีอยู่ทั้งหมด

Method Description Try it
.dropdown("toggle") Toggles the dropdown. If set, it will open the dropdown menu by default
.dropdown("update") Updates the position of an element's dropdown
.dropdown("dispose") Destroys an element's dropdown

เหตุการณ์แบบเลื่อนลง

ตารางต่อไปนี้แสดงรายการเหตุการณ์แบบเลื่อนลงที่มีอยู่ทั้งหมด

Event Description Try it
show.bs.dropdown Occurs when the dropdown is about to be shown.
shown.bs.dropdown Occurs when the dropdown is fully shown (after CSS transitions have completed)
hide.bs.dropdown Occurs when the dropdown is about to be hidden
hidden.bs.dropdown Occurs when the dropdown is fully hidden (after CSS transitions have completed)

เคล็ดลับ:ใช้ jQuery's event.relatedTarget เพื่อรับองค์ประกอบที่เรียกดรอปดาวน์:

ตัวอย่าง

$(".dropdown").on("show.bs.dropdown", function(event){
  var x = $(event.relatedTarget).text(); // Get the text of the element
  alert(x);
});