jQuery sort elements using data id

前端 未结 3 1222
我寻月下人不归
我寻月下人不归 2020-11-28 11:50

I have an HTML structure as follows:

3条回答
  •  醉梦人生
    2020-11-28 12:30

    A more generic function to sort elements using jQuery:

    $.fn.sortChildren = function (sortingFunction: any) {
    
        return this.each(function () {
            const children = $(this).children().get();
            children.sort(sortingFunction);
            $(this).append(children);
        });
    
    };
    

    Usage:

    $(".clist").sortChildren((a, b) => a.dataset.sid > b.dataset.sid ? 1 : -1);
    

提交回复
热议问题