jQuery insert div as certain index

后端 未结 9 693
眼角桃花
眼角桃花 2020-12-07 19:38

Say I have this:

1
2

bu

9条回答
  •  攒了一身酷
    2020-12-07 20:16

    This one works best for me,

    function SetElementIndex(element, index) {
                var Children = $(element).parent().children();
                var target = Children[index];
    
                if ($(element).index() > index) {
                    if (target == null) {
                        target = Children[0];
                    }
                    if (target != element && target != null) {
                        $(target).before(element);
                    }
                } else {
                    if (target == null) {
                        target = Children[Children.length - 1];
                    }
                    if (target != element && target != null) {
                        $(target).after(element);
                    }
    
                }
            };
    

提交回复
热议问题