Jquery's method for getting the first few elements

Source: Internet
Author: User

Jquery is often used. After selecting a group of elements, you need to find the first element in this group.
In jquery, the eq () method is used to locate the nth element or the nth element. The usage of eq () in jquery is as follows:
The eq () selector selects an element with the specified index value.
The index value starts from 0, and the index value of all the first element is 0 (not 1 ).
It is often used with other elements/selectors to select the element with a specific sequence number in the specified group.

Example:

The code is as follows: Copy code

Certificate ('{test'}.children().eq(1).css ({'display': 'inline-block '});

Set the second child element style of the element whose id is test to 'display': 'inline-block '.

Another method

The code is as follows: Copy code

$ (": Eq (index )")
For example: $ ("p: eq (1 )")

Example of another method

The code is as follows: Copy code

<Script type = "text/javascript" src = "/jquery-latest.js"> </script>
<Script>
$ (Function (){
$ ('A'). each (function (I ){
This. onclick = function (){
Alert (I );
Return false;
};
});
});
</Script>
<A href = ""> Baidu </a>
<A href = ""> google </a>
<A href = "http://www.111cn.net"> msn </a>
<A href = ""> qq </a>

Or write it like this.

The code is as follows: Copy code

<Script type = "text/javascript" src = "jquery-1.1.3.1.js"> </script>
<Script type = "text/javascript">
$ (Function ()
    {
$ ("A"). bind ("click", function ()
      {
Alert ($ ("a"). index (this ));
      }
      )
    }
    )
</Script>

The effect is the same.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.