我正在尝试按距离对html列表进行排序,我不想将距离属性放入我的标记中。I do to put distance property in my markup。我所做的是渲染商店列表html,在js中我创建了一个保存商店和距离的数组。
locations = [];
locations.push({
'store': stores[i],
'cordinate': Math.random()
});
我用这个对它进行排序
locations.sort(dynamicSort('cordinate'));
function dynamicSort(property) {
var sortOrder = 1;
return function(a, b) {
var result = (a[property] < b[property]) ? -1 : (a[property] > b[property]) ? 1 : 0;
return result * sortOrder;
}
}
这就是我被卡住的地方,我如何使用这个数组对html进行排序?这里的关键是,我不想因为在那里设置坐标而弄乱我的html。
编辑
这个问题是关于使用数组中填充的排序顺序对输出html进行排序。在那里,我删除了html和其他js,我只是因为懒于填充标记而使用它们。
发布于 2016-10-18 16:42:36
基本上,您需要做的就是按照排序顺序在html元素上调用append。
这会将元素附加到html元素的末尾。
var sortHTML = function(locations) {
$.each(locations, function(i, location) {
var targetElement = $('#' + location.store.id);
targetElement.parent().append(targetElement)
});
};
sortHTML(locations);
https://stackoverflow.com/questions/40103123
复制相似问题