search

Home  >  Q&A  >  body text

javascript - After the jquery object is defined as a jquery variable, how to use: gt this (there may be a problem with the expression)

$("li:gt(2)") can be used, but a long time ago I saw a jquery variable converted to XX and then used:gt(2), such as var $li=$(" li"), I need to use: gt(2) based on the $li variable. I don’t know if you understand what I mean. I am looking for an answer. I saw it on a blog a long time ago, but I can’t remember it.

黄舟黄舟2757 days ago597

reply all(2)I'll reply

  • ringa_lee

    ringa_lee2017-05-16 13:37:42

    $li.filter(':gt(2)')

    Is this so?

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-05-16 13:37:42

    jquery itself does not have this method, only this selector, but you can extend it

    $.fn.gt = function(num){
        var after = [];
        this.each(function(i){
            if(i>=num){
               after.push(this);
            }
        });
        return after;
    }
    $.fn.lt = function(num){
        var before = [];
        this.each(function(i){
            if(i<num){
               before.push(this);
            }
        });
        return before;
    }

    reply
    0
  • Cancelreply