Khi thao tác với DOM, không phải lúc nào bạn cũng cần tìm tổ tiên (ancestor) hay con cháu (descendant).
Đôi lúc, bạn cần di chuyển qua các phần tử ngang hàng (siblings) — những phần tử có chung cha mẹ, hoặc lọc ra những phần tử cụ thể theo điều kiện.
jQuery cung cấp bộ phương thức traversing siblings và filtering rất mạnh mẽ, giúp bạn:
- Dễ dàng di chuyển giữa các phần tử cùng cấp.
- Lọc phần tử theo điều kiện chính xác nhanh chóng.
Trong bài viết này, chúng ta sẽ khám phá:
- Các phương thức traversing siblings như
siblings(),next(),nextAll(),prev(),prevAll() - Các phương thức filtering như
first(),last(),eq(),filter(),not() - Kèm theo ví dụ minh họa trực quan
1. Traversing Siblings (Đi tới phần tử ngang hàng)
1.1. siblings()
- Trả về tất cả phần tử ngang hàng với phần tử được chọn (có cùng parent).
Cú pháp:
$(selector).siblings();
$(selector).siblings(filter);Ví dụ:
Learn Python syntax easily with this beginner-friendly guide. Understand basic syntax rules, variables, loops,...
Struggling with the “Key is already in use” error when adding an SSH key to GitHub? This guide walks you through the...
Learn how to get URL segments in Laravel using Request::segment() and segments() methods. Step-by-step examples and best...
Struggling with the “Key is already in use” error when adding an SSH key to GitHub? This guide walks you through the...
Learn how to clear cache in Laravel 12 for Routes, Views, Config, Events, and more. Detailed guide on Artisan commands...
Learn how to install Python quickly and easily with this step-by-step guide for Windows, macOS, and Linux. Start your...
Learn Python syntax easily with this beginner-friendly guide. Understand basic syntax rules, variables, loops,...
Learn how to use CSS offset-path, offset-position, offset-distance, and offset-rotate properties to create smooth and...
