How to check if the element is not the first-child?

前端 未结 10 1939
北荒
北荒 2021-01-01 10:26

How do you know if the current element is not the first-child?

It should work with $(this), for example:

$(\"li\").click(function(e) {
          


        
相关标签:
10条回答
  • 2021-01-01 11:25

    Check the index

    $(this).index() == 0 // is first
    
    0 讨论(0)
  • 2021-01-01 11:28

    You could simply ask for its .index() position (relative to its siblings).

    $(this).index();   // returns a zero based index position
    
    0 讨论(0)
  • 2021-01-01 11:29

    You can do this just to test an element if it's the first child: $(this).is(':first-child'). Other selectors like :last-child would work too.

    0 讨论(0)
  • 2021-01-01 11:31

    Keep it simple, use the DOM

    
    $("li").click(function(e) {
    
        if (this.previousSibling != null)
        {
            /* do something */
        }
    
    });
    
    0 讨论(0)
提交回复
热议问题