如何更改匹配的jQuery集合中元素的顺序
How to change the order of elements in a matched jQuery set
我正在编写一个基于javascript的过滤器:
有很多类为.single
的div包含多个文本元素。过滤器已经隐藏了所有不包含单词的.single
:
//take them off dom, manip and put them back
singles.detach().each(function(i) {
var $this = $(this);
//make sure everything is visible
$this.show();
//if this template is not included OR if this index number does not meet the treshold
if($.inArray(i, included) == -1 || treshold >= templates[i].score) {
$this.hide();
}
}).appendTo(container);
数组included
包含.single
的所有索引,这些索引至少包含一个字。数组templates
包含具有每个.single
的所有文本的对象以及基于相关性和字数的分数。例如:
templates = [
{ text: ['long string', 'long string 2'], score: 5 },
{ text: ['sf sd', 'gdasd'], score: 3 }
]
(所以所有的索引都不会发生在DOM中)
现在我想要的是根据这个分数对集合进行排序,或者换句话说:顶部的最高分数。集合中每个元素的索引与templates
中的索引相同(包含所有分数)。
如果将分数作为数据()添加到$.each 中的元素
$(this).data('score', templates[i].score);
然后你可以使用分数对进行排序
function scoreSort(a, b) {
return $(a).data('score') > $(b).data('score') ?1 :-1;
}
singles.sort( scoreSort);
/* now replace html*/
container.append( singles);
我可能遗漏了一些东西,你说你希望它们按templates
的索引顺序排列,但它们必须已经按该顺序排列,你才能使用$.中的索引查看templates
中的分数。每个
相关文章:
- 按我自己的类克隆另一个元素的内容和顺序
- 重新排列HTML元素的顺序并更改内容
- 在Javascript中列出顺序子集元素
- HEAD中LINK和STYLE元素的求值顺序
- 如何在php中按元素按字母顺序排列json文件
- Javascript - 如何按顺序从数组中挑选随机元素
- 使用 KNOCKOUT.js 在禁用元素的 HTML 中保持 Tab 键顺序
- 如何将.append()创建的元素顺序添加到先前添加的元素中
- 排列<p>使用jQuery按日期顺序排列的元素
- 通过普通JS更改元素的索引(顺序)
- 按随机顺序对元素进行动画处理
- 在不使用jQuery sortable的情况下警告丢弃元素的顺序
- 重新排列元素顺序响应式设计
- 导致错误的数组元素顺序
- MongoDB的元素顺序
- querySelectorAll元素顺序
- 使用jQuery改变元素顺序的最优方法
- 保持元素顺序(select .order)与退出转换在d3
- mootools clone() inject('top') 颠倒了元素顺序
- 用CSS改变DOM元素顺序(鳄鱼)