0
我寫一個基於JavaScript的過濾器:如何更改匹配的jQuery元素的順序設置
一個大量的div有與包含多個文本元素的類.single
。該過濾器已經隱藏了所有.single
「不包含單詞S:
//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
所有索引」是至少含有一個一個字S。數組templates
包含每個.single
包含所有文本的對象以及基於相關性和單詞數量的分數。例如:
templates = [
{ text: ['long string', 'long string 2'], score: 5 },
{ text: ['sf sd', 'gdasd'], score: 3 }
]
(因此,所有的索引不會在DOM發生)
現在,我要的是在此基礎上的分數,或者換句話說集合進行排序:上得分最高最佳。該集合中的每個元素的索引與templates
(其中包含所有分數)中的索引相同。
我使用$(elem).each()而不是$ .each ()。模板和集合已經在相同的順序,所以我可以使用'data'綁定分數(感謝提示)。然而,你建議的排序並不按比分排序,而是顯示不同的結果(我不明白爲什麼)。 – askmike 2012-02-29 16:12:49
我嘗試了一個簡單的演示http://jsfiddle.net/bKfwv/ – charlietfl 2012-02-29 16:16:21
你是對的,它的工作原理。那必須是我的其他代碼。感謝您的解決方案! – askmike 2012-02-29 16:18:32