jQuery to find all previous elements that match an expression

后端 未结 5 2045
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-05 16:21

Using jQuery, how do you match elements that are prior to the current element in the DOM tree? Using prevAll() only matches previous siblings.<

5条回答
  •  隐瞒了意图╮
    2020-12-05 16:31

    I usually number elements (1,2,3..) (rel="number"), so then i use this code to give class to all previous elements:

    var num = $(this).attr("rel");
    
    for (var i = 1; i<=num; i++)
    {
        $('.class[rel="'+i+'"]').addClass("newclass");
    }
    

提交回复
热议问题