Discuss / JavaScript / 两种方法.

两种方法.

Topic source

~曉瘋~

#1 Created at ... [Delete] [Delete and Lock User]

方法一(偷懒): 把原来的全部删掉然后重新添加一便.

var li = $('#test-div>ul>li');

li.remove();

var ul = $('#test-div>ul');

var data = ['JavaScript','Lua','Pascal','Python','Ruby','Swift'];

for (let i of data) ul.append($('<li><span>' + i + '</span></li>'));

方法二 (正常): 把新元素的加进去.然后直接对元素进行排序.

var ul = $('#test-div>ul');

var data = ['Lua','Pascal','Ruby'];

for (let i of data) ul.append($('<li><span>' + i + '</span></li>'));

var ul = $('#test-div>ul');

sort = Array.from(ul.children()).sort((a, b) => a.innerText > b.innerText ? 1:-1);

for (let e of sort) ul.append(e);


  • 1

Reply