对 $(this).index() 的一点见解

505 阅读1分钟
原文链接: blog.csdn.net

版权声明:本文为博主原创文章,未经博主允许不得转载。

参考:www.w3cfuns.com/notes/18316…




    
        
        菜鸟教程(runoob.com)
        

        
    

    

        

Coffee

哈哈哈哈哈哈哈

哈哈哈哈哈哈哈

哈哈哈哈哈哈哈

Milk

哈哈哈哈哈哈哈

哈哈哈哈哈哈哈

哈哈哈哈哈哈哈

Soda

哈哈哈哈哈哈哈

哈哈哈哈哈哈哈

哈哈哈哈哈哈哈

MD,还以为是jQuery BUG,修改




    
        
        菜鸟教程(runoob.com)
        

        
    

    

        

Coffee

哈哈哈哈哈哈哈

哈哈哈哈哈哈哈

哈哈哈哈哈哈哈

Milk

哈哈哈哈哈哈哈

哈哈哈哈哈哈哈

哈哈哈哈哈哈哈

Soda

哈哈哈哈哈哈哈

哈哈哈哈哈哈哈

哈哈哈哈哈哈哈

1.$(this).index()的理解

$(selector).index()获得第一个匹配元素相对于其同级元素的 index 位置。例如:









Click the button to get the index of the element with id="favorite", relative to the jQuery selector (class="hot"):

Get index
  • Milk
  • Tea
  • Coffee

点击第一个拥有hot的li返回的index是1

而$(selector).index(element)获得元素相对于选择器的 index 位置。该元素可以通过 DOM 元素或 jQuery 选择器来指定。例如:









Click the button to get the index of the element with id="favorite", relative to the jQuery selector (class="hot"):

Get index
  • Milk
  • Tea
  • Coffee

点击第一个hot的li返回的是index为0