你说你可以修改文件的内容,所以我建议将其转换为JSON(和分离的日期/时间)。
[{"date": "2012/02/11", "time": "10:00:00", "title": "EventStart", "color": "Red"}, {"date": "2012/02/11", "time": "10:10:00", "title": "EventStop", "color": "Green"}]
然后,您可以使用getJSON
来获取/解析它。
jQuery.getJSON('schedule.txt',function(data){
// data is an array of objects
$.each(data, function(){
console.log(this.title); // log each title
});
});
从这里,你可以阅读时间,并找出哪一个是最新的。像这样的东西应该工作:
if(Date.now() <= Date.parse(this.date+' '+this.time))
所以,把他们放在一起:
jQuery.getJSON('schedule.txt',function(data){
var matchedSchedule = {};
// data is an array of objects
$.each(data, function(){
if(Date.now() <= Date.parse(this.date+' '+this.time)){
matchedSchedule = this; // If time matches, set a variable to this object
return false; // break the loop
}
});
console.log(matchedSchedule.title);
// to set a "global" variable, add it to `window`
window.eventTitle = matchedSchedule.title;
});
它看起来像一个东西,应该在服务器上完成,而不是客户端。 – gdoron 2012-02-14 21:41:17
@gdoron同意。 – rcdmk 2012-02-14 21:44:04
@gdoron我同意,但在这种特殊情况下,它需要在客户端完成。 – codybuell 2012-02-14 21:48:39