JQUERY SELECTOR
Cách dùng All Selector (“*”) trong jQuery Cách dùng :animated Selector trong jQuery Cách dùng Attribute Contains Prefix Selector [name|=”value”] trong jQuery Cách dùng Attribute Contains Selector [name*=”value”] trong jQuery Cách dùng Attribute Contains Word Selector [name~=”value”] trong jQuery Cách dùng Attribute Ends With Selector [name$=”value”] trong jQuery Cách dùng Attribute Equals Selector [name=”value”] trong jQuery Cách dùng Attribute Not Equal Selector [name!=”value”] trong jQuery Cách dùng Attribute Starts With Selector [name^=”value”] trong jQuery Cách dùng :button Selector trong jQuery Cách dùng :checkbox Selector trong jQuery Cách dùng :checked Selector trong jQuery Cách dùng Child Selector (“parent > child”) trong jQuery Cách dùng Class Selector (“.class”) trong jQuery Cách dùng :contains() Selector trong jQuery Cách dùng Descendant Selector (“ancestor descendant”) trong jQuery Cách dùng :disabled Selector trong jQuery Cách dùng Element Selector (“element”) trong jQuery Cách dùng :empty Selector trong jQuery Cách dùng :enabled selector trong jQuery Cách dùng :eq() Selector trong jQuery Cách dùng :even Selector trong jQuery Cách dùng :odd Selector trong jQuery Cách dùng :file Selector trong jQuery Cách dùng :first-child Selector trong jQuery Cách dùng :first-of-type Selector trong jQuery Cách dùng :first Selector trong jQuery Cách dùng :focus Selector trong jQuery Cách dùng :gt() Selector trong jQuery Cách dùng Has Attribute Selector [name] trong jQuery Cách dùng :has() Selector trong jQuery Cách dùng :header Selector trong jQuery Cách dùng :hidden Selector trong jQuery Cách dùng ID Selector (“#id”) trong jQuery Cách dùng :image Selector trong jQuery Cách dùng :input Selector trong jQuery Cách dùng :lang() Selector trong jQuery Cách dùng :last-child Selector trong jQuery Cách dùng :last-of-type Selector trong jQuery Cách dùng :last Selector trong jQuery Cách dùng :lt() Selector trong jQuery Cách dùng Multiple Attribute Selector [name=”value”][name2=”value2″] trong jQuery Cách dùng Multiple Selector (“selector1, selector2, selectorN”) trong jQuery Cách dùng Next Adjacent Selector (“prev + next”) trong jQuery Cách dùng Next Siblings Selector (“prev ~ siblings”) trong jQuery Cách dùng :not() Selector trong jQuery Cách dùng :nth-child() Selector trong jQuery Cách dùng :nth-last-child() Selector trong jQuery Cách dùng :nth-of-type() Selector trong jQuery Cách dùng :nth-last-of-type() Selector trong jQuery Cách dùng :only-child Selector trong jQuery Cách dùng :only-of-type Selector trong jQuery Cách dùng :parent Selector trong jQuery Cách dùng :password Selector trong jQuery Cách dùng :radio Selector trong jQuery Cách dùng :reset Selector trong jQuery Cách dùng :root Selector trong jQuery Cách dùng :selected Selector trong jQuery Cách dùng :submit Selector trong jQuery Cách dùng :target Selector trong jQuery Cách dùng :text Selector trong jQuery Cách dùng :visible Selector trong jQuery
JQUERY EVENTS
JQUERY API
CÁC CHỦ ĐỀ
BÀI MỚI NHẤT
MỚI CẬP NHẬT

Cách dùng :eq() Selector trong jQuery

Eq() Selector sẽ lựa chọn phần tử html nằm ở vị trí được cung cấp.

test php

banquyen png
Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.

Các index-related selectors (:eq(), :lt(), :gt(), :even, :odd) sẽ lọc lại kết quả trong tập hợp kết quả mà một selector đứng trước nó trả về. Chúng sẽ làm thu hẹp các kết quả chính xác nhất theo ý của người sử dụng. Ví dụ, 
Nếu các phần tử được lựa chọn lần đầu với selector (.myClass) và bốn phần tử được trả về, các phần tử này được đánh các chỉ số từ 0 đến 3 sau đó có thể sử dụng Eq() Selector để lọc lấy kết quả chính xác nhất.

Lưu ý rằng, cũng giống với các chỉ số trong một mảng, phần tử đầu tiên trong bộ kết quả trả về sẽ có chỉ số là 0, phần tử thứ hai là 1 ..vv. Bạn cs thể sử dụng các chỉ số âm để lấy các kết quả cuối cùng, tuy nhiên từ trước phiên bản Jquery 1.8 các chỉ số âm sẽ không được hỗ trợ.

Chú ý: Bởi vì :Eq() là một Jquery extension và không phải là một phần thiết lập của CSS, các truy vấn sử dụng :Eq() sẽ không thể phát huy hiệu quả của phương thức querySelectorAll(). Để đạt được hiệu quả cao nhất khi sử dụng bạn nên sử dụng $("your-pure-css-selector").eq(index) để thay thế.

Cú pháp

Cú pháp
Query( ":eq(index)" )

Trong đó:

Bài viết này được đăng tại [free tuts .net]

  • index là chỉ số của phần tử muốn chọn.

Ví dụ

Tìm kiếm thẻ div thứ 2 và đổi màu nền cho nó:

Code RUN
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Học lập trình miễn phí tại freetuts.net</title>
        <script src="https://freetuts.net/cnd/js/jquery/jquery-3.2.1.min.js"></script>
    </head>
    <body>
        <h1>Học lập trình miễn phí tại freetuts.net</h1>
    
        <div>
            <input type="text" name="text">
            <input type="text" name="text">
        </div>
        <div>
            Đây là thẻ div thứ 2
        </div>
        <input type="text" name="test" disabled="disabled">
        <input type="button" name="button" value="Button" disabled="disabled">
        <h2>This is H2 content</h2>
        <button onclick="myFunction()">Click vào đây để xem kết quả</button>
        <script>
            function myFunction(){
                $( "div" ).eq(1).css( "background", "red");
            }
        </script>
    </body>
</html>

Kết quả:

 

Tham khảo: jquery.com

Cùng chuyên mục:

Cách dùng .bind() trong JQuery

Cách dùng .bind() trong JQuery

Cách dùng .delegate() trong JQuery

Cách dùng .delegate() trong JQuery

slideUp() và slideDown() trong jQuery

slideUp() và slideDown() trong jQuery

Nếu nói đến hiệu ứng slide thì ta phải tìm đến những ...

fadeIn() và fadeOut() trong jQuery

fadeIn() và fadeOut() trong jQuery

Không giống như show và hide, fadeIn và fadeOut sẽ hiển thị hoặc mờ ...

Show() và Hide() trong jQuery

Show() và Hide() trong jQuery

Hàm hide chỉ đơn giản là thiết lập thuộc tính ...

jQuery selector là gì? Trọn bộ selector trong jQuery đầy đủ nhất

jQuery selector là gì? Trọn bộ selector trong jQuery đầy đủ nhất

Trong bài này chúng ta sẽ tìm hiểu về Selector trong jQuery, đây là bài…

jQuery là gì? Cách viết jQuery cho người mới bắt đầu

jQuery là gì? Cách viết jQuery cho người mới bắt đầu

Trong công nghệ web 2.0 thì Javascript là một

Cách ẩn hiện nội dung khi click vào button trong jQuery

Cách ẩn hiện nội dung khi click vào button trong jQuery

Cách dùng All Selector (“*”) trong jQuery

Cách dùng All Selector (“*”) trong jQuery

Cách dùng All Selector (“*”) trong jQuery

Cách dùng :animated Selector trong jQuery

Cách dùng :animated Selector trong jQuery

Cách dùng :animated Selector trong jQuery

Cách dùng Attribute Contains Prefix Selector [name|=”value”] trong jQuery

Cách dùng Attribute Contains Prefix Selector [name|=”value”] trong jQuery

Cách dùng Attribute Contains Prefix Selector [name|=”value”] trong jQuery

Cách dùng Attribute Contains Selector [name*=”value”] trong jQuery

Cách dùng Attribute Contains Selector [name*=”value”] trong jQuery

Cách dùng Attribute Contains Selector [name*=”value”] trong jQuery

Cách dùng Attribute Contains Word Selector [name~=”value”] trong jQuery

Cách dùng Attribute Contains Word Selector [name~=”value”] trong jQuery

Cách dùng Attribute Contains Word Selector [name~=”value”] trong jQuery

Cách dùng Attribute Ends With Selector [name$=”value”] trong jQuery

Cách dùng Attribute Ends With Selector [name$=”value”] trong jQuery

Cách dùng Attribute Ends With Selector [name$=”value”] trong jQuery

Cách dùng Attribute Equals Selector [name=”value”] trong jQuery

Cách dùng Attribute Equals Selector [name=”value”] trong jQuery

Cách dùng Attribute Equals Selector [name=”value”] trong jQuery

Cách dùng Attribute Not Equal Selector [name!=”value”] trong jQuery

Cách dùng Attribute Not Equal Selector [name!=”value”] trong jQuery

Cách dùng Attribute Not Equal Selector [name!=”value”] trong jQuery

Cách dùng Attribute Starts With Selector [name^=”value”] trong jQuery

Cách dùng Attribute Starts With Selector [name^=”value”] trong jQuery

Cách dùng Attribute Starts With Selector [name^=”value”] trong jQuery

Cách dùng :button Selector trong jQuery

Cách dùng :button Selector trong jQuery

Cách dùng :button Selector trong jQuery

Cách dùng :checkbox Selector trong jQuery

Cách dùng :checkbox Selector trong jQuery

Cách dùng :checkbox Selector trong jQuery

Cách dùng :checked Selector trong jQuery

Cách dùng :checked Selector trong jQuery

Cách dùng :checked Selector trong jQuery

Top