Check if element is before or after another element in jQuery

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

Let us suppose I have this markup

First

Hi

Hello

Second

9条回答
  •  误落风尘
    2020-12-23 16:50

    Rocket's solution works fine if sel is a real selector, if you pass a jquery object though, it won't work.

    If you want a plugin that works with both selectors and jquery objects, just use my slightly modified version instead:

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

    Kudos to Rocket for the original solution.

提交回复
热议问题