Usage Analysis of index () in jQuery, jqueryindex

Source: Internet
Author: User

Usage Analysis of index () in jQuery, jqueryindex

This document describes the usage of index () in jQuery. Share it with you for your reference. The specific method is as follows:

Now there is a problem: if there are more than N list items in the list, I want to know which one should I click?

In this regard, jQuery provides an index () method:

index(subject)

This method searches for elements that match the object indicated by parameters and returns the index value of the corresponding element.

If a matching element is found, return from 0; if no matching element is found, return-1.

However, the example provided in the API does not seem to be correct. The example is as follows:

<Ul> <li> <a href = "# nogo"> here is a sequence </a> </li> <a href = "# nogo"> here is a sequence </a> </li> <a href = "# nogo"> here is a sequence </a> </li> 

As shown above, this is an unordered list. How can I click any list project to obtain the project sequence?

The implementation method is as follows:

$(document).ready(function(){ $("#act li").click(function(){  alert( $( "#act li" ).index( $(this)[0] ) );  })}) 

Here:

$( "#act li" ).index( $(this)[0] )

Very important!


Jquery index () Usage

Do not repeat the id of td.

The detailed usage of jquery index, how does jquery retrieve the index?

// 1. Pass a DOM object and return the index position of this object in the original set $ ('lil '). index ($ ('# bar'); // 1, passing a jQuery object $ ('lil '). index ($ ('li: gt (0) '); // 1, passing a set of jQuery objects, returns the index position of the first element in this object in the original set $ ('# bar '). index ('lil'); // 1. Pass a selector and return # position of bar in all li. // 1. No parameter is passed, returns the index position of this element in the same generation.

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.