Javascript 如果html相同,则隐藏
我正在使用Feedburner显示提要。有时提要具有相同的标题。在这种情况下,我只想显示第一个标题,并隐藏具有相同文本的所有其他标题。我试过这个:Javascript 如果html相同,则隐藏,javascript,jquery,html,events,if-statement,Javascript,Jquery,Html,Events,If Statement,我正在使用Feedburner显示提要。有时提要具有相同的标题。在这种情况下,我只想显示第一个标题,并隐藏具有相同文本的所有其他标题。我试过这个: 不走运。我可以把它们称为“a”,但我不知道如何区分它们 尝试从显示所有链接开始,并使用以下javascript: $(function() { var $feeds = $(".feedburnerFeedBlock li a"); $feeds.each(function(i) { var $that = $(thi
不走运。我可以把它们称为“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();
}
});
});
});
$(函数(){
变量$feeds=$(“.feedburnerFeedBlock li a”);
$feeds.每个(功能(i){
var$that=$(this);
$feeds.每个(功能(j){
var$this=$(this);
如果(j而不是使用filter
函数,您可以使用一个对象来收集所有提要标题及其jQuery元素。该对象的行为就像Java中的HashMap
,因为对象不能包含重复的键,所以重复的提要标题会自动消除
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:
此外,由于多种原因,您的代码无法工作。除此之外,jQuery的.html()
函数只返回集合()中第一个元素的html字符串.它不会在输出中显示任何内容,这不是因为我有display:none…..应该可以帮助您找出如何从结果数组中筛选唯一元素。@goldenparrot谢谢+1,我在尝试过它,但似乎无法“显示”var unique…也许您可以帮助我(回答)