Check if element is before or after another element in jQuery

后端 未结 9 1593
花落未央
花落未央 2020-12-23 16:23

Let us suppose I have this markup

First

Hi

Hello

Second

9条回答
  •  情书的邮戳
    2020-12-23 16:52

    To see if an element is after another, you can use prev() or prevAll() to get the previous element(s), and see if it matches.

    And to see if an element is before another, you can use next() or nextAll() to get the next element(s), and see if it matches.

    $.fn.isAfter = function(sel){
      return this.prevAll(sel).length !== 0;
    }
    $.fn.isBefore= function(sel){
      return this.nextAll(sel).length !== 0;
    }
    

    DEMO: http://jsfiddle.net/bk4k7/

提交回复
热议问题