Web Cơ Bản

Bài 03: jQuery Selectors

jQuery Selectors là gì?

jQuery Selectors là một trong những phần quan trọng nhất của thư viện jQuery.

jQuery Selectors được dùng để chọn các phần tử HTML mà bạn muốn thao tác dựa trên id, class, thuộc tính, giá trị thuộc tính, quan hệ giữa các phần tử....

Cú pháp

Để chọn một phần tử HTML chúng ta sử dụng cú pháp

$(selector)
Ví dụ 1:

Đoạn mã dưới đây chọn tất cả các phần tử <p>


$("p")
Ví dụ 2:

Đoạn mã dưới đây chọn tất cả các phần tử có class là abc


$(".abc")
Ví dụ 3:

Đoạn mã dưới đây chọn tất cả các phần tử <div> có class là abc


$("div.abc")
Ví dụ 4:

Đoạn mã dưới đây chọn phần tử có id là abc rồi ẩn đó đi


$("#abc").hide();
Xem ví dụ

Tham khảo tất cả các bộ chọn (Selectors) trong jQuery

Dưới đây là một bảng tổng hợp các bộ chọn (Selectors) trong jQuery, bạn có thể tìm hiểu chi tiết của bộ chọn đó bằng cách bấm nào "Xem chi tiết"

Selector Ví dụ Mô tả Xem chi tiết
*    
#id    
.class    
.class,.class    
element    
el1,el2,el3    
:first    
:last    
:even    
:odd    
:first-child    
:first-of-type    
:last-child    
:last-of-type    
:nth-child(n)    
:nth-last-child(n)    
:nth-of-type(n)    
:nth-last-of-type(n)    
:only-child    
:only-of-type    
parent > child    
parent descendant    
element + next    
element ~ siblings    
:eq(index)    
:gt(no)    
:lt(no)    
:not(selector)    
:header    
:animated    
:focus    
:contains(text)    
:has(selector)    
:empty    
:parent    
:hidden    
:visible    
:root    
:lang(language)    
[attribute]    
[attribute=value]    
[attribute!=value]    
[attribute$=value]    
[attribute|=value]    
[attribute^=value]    
[attribute~=value]    
[attribute*=value]    
:input    
:text    
:password    
:radio    
:checkbox    
:submit    
:reset    
:button    
:image    
:file    
:enabled    
:disabled    
:selected    
:checked