我正在使用Feedburner來顯示提要。有時飼料有相同的標題。在這種情況下,我想只顯示第一個標題,並隱藏所有其他標題和相同的文本。我試過這樣:JsFiddle如果html相同,則隱藏
沒有運氣。我可以將它們稱爲'a',但我不明白如何區分它們。
我正在使用Feedburner來顯示提要。有時飼料有相同的標題。在這種情況下,我想只顯示第一個標題,並隱藏所有其他標題和相同的文本。我試過這樣:JsFiddle如果html相同,則隱藏
沒有運氣。我可以將它們稱爲'a',但我不明白如何區分它們。
開始嘗試與顯示各個環節,這個JavaScript:
$(function() {
var $feeds = $(".feedburnerFeedBlock li a");
$feeds.each(function(i) {
var $that = $(this);
$feeds.each(function(j) {
var $this = $(this);
if (j <= i) {
return true;//continue
};
if ($this.text() == $that.text()) {
$this.hide();
}
});
});
});
除了使用filter
功能,你可以使用一個對象來收集所有與他們的jQuery元素的飼料標題。該對象的行爲與Java中的HashMap
類似,因爲對象不能包含重複鍵 - 所以重複的Feed標題將自動消除。
var unique = { };
// Reverse elements to keep first occurence of feed title (and not the last one)
$($(".feedburnerFeedBlock li").get().reverse()).each(function(){
// Use feed title as key and jQuery element as value
unique[$(this).find("a").text()] = $(this);
}).hide();
// Show all unique elements
for (title in unique) {
unique[title].show();
}
的jsfiddle:http://jsfiddle.net/Aletheios/9GKBH/1/
此外,你的代碼不會因爲幾個原因工作。其中jQuery的.html()
函數只返回集合中第一個元素的HTML字符串(see documentation)。
它不顯示任何東西在輸出 – 2619 2012-08-16 10:43:24
That't,因爲我有顯示:無..... – Youss 2012-08-16 10:44:34
http://stackoverflow.com/questions/5381621/jquery-function-to-get-all-unique-元素從數組 應該幫助你弄清楚如何從結果數組中過濾獨特的元素。 – Prasanth 2012-08-16 10:47:54