Jquery obtains the index number of the current element in the Peer element.

Source: Internet
Author: User

How to Use jquery to obtain the index number of the current element in the same generation element is described in the following example:

<ul class="img"><li><a href="#"></a></li><li><a href="#"></a></li><li><a href="#"></a></li><li><a href="#"></a></li><li><a href="#"></a></li></ul>

If I bind a click event to each img element, how can I know which img I am currently clicking in the click function? That is, how does one know its index number? (For example, if a single ad1.jpg request is returned, the return value 0, ad2.jpg, or the first return value 1 and 2) is as follows:

$('.img img').bind('click',function(){    alert($('.img img').index(this));});

In addition, you can directly use:

$(this).index();

This method gets the index number. After testing, the second method is more concise, and the obtained index number is relative to the current parent element, rather than the first method relative to the entire body element.

Articles you may be interested in
  • How to insert an element to the front of the body element in js
  • How does php Delete the first and last elements of an array?
  • Jquery obtains the set of peer elements, and summarizes the usage of jquery siblings
  • How to obtain the complete url address and parameters in javascript
  • Windows cannot start the hardware device because its configuration information (in the Registry) is incomplete or damaged. (Code 19) Solution
  • JavaScript retrieves the current date and time and displays the day of the week at the same time
  • Mysql database cache function analysis, debugging, and performance summary
  • JavaScript calculates the number of characters currently entered

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.