Check if element is before or after another element in jQuery

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

Let us suppose I have this markup

First

Hi

Hello

Second

9条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-23 17:06

    You can use the index() function:

    $('p').each(function(){
       var index = $(this).index();
       if(index > $("#first").index() && index < $("#second").index()) {
         // do stuff
       }
    });
    

提交回复
热议问题