Why doesn't index() working as expected

前端 未结 5 1640
余生分开走
余生分开走 2021-01-22 18:09

HTML:

Div 1
Div 2
Div 3
<
5条回答
  •  花落未央
    2021-01-22 18:48

    index() gives index of element with respect to its siblings. The first div with class text is 5th element and has index 4, br is at 5 index and next div with class test has index 6 and so on. We can check the indexes of each element by the following demo

    Live Demo

    $("#parent1 *").each(function () {
        alert("TagName >> " + this.tagName + ", Index >> " + $(this).index() + ", Text " + $(this).text());
    });
    

    If no argument is passed to the .index() method, the return value is an integer indicating the position of the first element within the jQuery object relative to its sibling elements, Reference.

提交回复
热议问题