【发布时间】:2016-03-19 07:14:54
【问题描述】:
我正在创建一个脚本来显示我的 rss 提要并过滤掉旧帖子,所有这些都使用 Jquery。在我的脚本中,我试图过滤掉超过 90 天的帖子。
有 1 个帖子显示超过 90 天的限制。我尝试使用不同的参数更改我的标签。
我遗漏或添加的不应该存在的内容?
我的 Jquery 代码是:
<script>
//function to load XML document
$.ajax({
type: 'GET',
url: '/ser/whatsnew_main.xml',
dataType: 'xml',
success: function(xml){
$(xml).find("item").each(function(){
var title = $(this).find('title').text();
var date = $(this).find('pubDate').text();
var description = $(this).find('description').text();
$('#feed').append('<li><strong>'+title+'</strong><br>'+date+'<br>'+description+'</li>');
//exp date parameters
var today = new Date();
var runOn = new Date(today.setDate(1));
var expire = new Date(runOn.setDate(-90));
var pub = new Date(date);
if(today.getDate() === 1 == true && pub < expire == true){
$(this).each('item').empty();
}
else if(pub < expire == true){
$(this).each('item').empty();
}
alert("on " +today.getDate()+ ", expiration date is " +expire);
});
}
});
</script>
我尝试使用if(today.getDate() === 26 == true && pub>expire == false) 的当前日期,我尝试了if(today.getDate()=== 26 == true && pub<expire == false)、if(today.getDate()=== 26 == true && pub<expire == true)、if(today.getDate()=== 26 == true && pub>expire == true),并且要么显示所有帖子,要么显示过去 90 天的 1 个帖子与较新的帖子帖子。
仅供参考,当我运行警报时,today.getDate() = 1 在本月 1 日,expire = Oct 2 2015,所以不知道为什么 2015 年 9 月 30 日的帖子不断出现。
更新 - 我的 xml 代码是(没有 <xml>、<rss> 和 <channel> 标签):
<item class="combo">
<title>Generic Title</title>
<pubDate>28 Oct 2015</pubDate>
<description><![CDATA[Content <a href="/file.htm">some link</a>.]]></description>
</item>
<item class="class">
<title>Gen title</title>
<pubDate>30 Sep 2015</pubDate>
<description><![CDATA[Some more content <a href="/file2.htm#anchor">another link</a>.]]></description>
</item>
我在其他 rss 提要上尝试了相同的代码,但无论发布日期是 2015 年 9 月 30 日还是 2015 年 8 月 21 日,它都会一直显示最后一个过时的帖子。
我不想创建一个全新的函数,从第一个函数调用它并创建一个全新的 xml 加载函数只是为了获取 pubDate。
【问题讨论】:
-
有助于查看一些商品的实际 Feed 数据,包括不应该显示的商品。
-
请尽量简化代码,=== 和 == 的求值已经是一个布尔值,类似于...if (today.getDate() == 26 && runOn > expire)
-
@Chris OnDaRocks,在尝试了您的建议后,如果我没有将“== true”添加到两个布尔值,我的代码会显示所有帖子。出于某种原因,它想要它在那里。我还用'== true'&'==false'和'==true'&'==true'尝试了你的建议,来回更改>,因为代码不起作用,仍然是所有帖子显示。我想我的下一个选择是创建一个单独的函数并在第一个函数结束后调用它。但这意味着我可能必须执行整个 xml 加载操作才能获取 pubDates。 :|
-
@adammtlx,我的 xml 代码示例是 - 不带有 beg
、 & 标签:' 通用标题 2015 年 10 月 28 日 一些链接.]]> item> ' 我正在删除最后一个过期的帖子,在这种情况下是 2015 年 9 月 30 日的 1。其他较旧的帖子已被删除。Gen title 2015 年 9 月 30 日 另一个链接。]]> -
@ChrisOnDaRocks,我在它们两个上都省略了 '==true' 部分,它们可以工作,但不能使用 'runOn>expire'。现在我正在使用'if(today.getDate() == 26 && pub
标签: javascript jquery rss