English 中文(简体)
Java 更改表罗
原标题:JavaScript Change Table Row Class based on Date

我在很少的网站上工作,为我的大学的一个部门提供一些资源。 一切都非常好,因此,我试图增加一点功能,使事情更加直观。

如果你想看到完全的超文本结构和其他一切,那么,这里的网页是:{链接被删除以保护个人内容}

基本上,今年夏天我们重新负责的每个营地都有一行的桌子。 这是所有罚款和死胎。 我现在要做的是强调目前届会中任何营地的行踪(通过增加“流动”这一类别)。 鉴于我在 Java学的经验有限,这是我提出的:

<script type="text/javascript" >
$(document).ready(function() {
    var today = new Date();

    var Traffic_Court_Start = new Date("May 31 2012 12:01 AM");
    var Traffic_Court_Stop = new Date("June 1 2012 11:59 PM");
    var Summer_Enrichment_Start = new Date("June 10 2012 12:01 AM");
    var Summer_Enrichment_Stop = new Date("June 16 2012 11:59 PM");
    var Wbb_Ind_Start = new Date("June 11 2012 12:01 AM");
    var Wbb_ind_Stop = new Date("June 14 2012 11:59 PM");
    var Soccer_Referee_Start = new Date("June 15 2012 12:01 AM");
    var Soccer_Referee_Stop = new Date("June 16 2012 11:59 PM");
    var Broadcast_Start = new Date("June 17 2012 12:01 AM");
    var Broadcast_Stop = new Date("June 21 2012 11:59 PM");
    var Tennis_1_Start = new Date("June 17 2012 12:01 AM");
    var Tennis_1_Stop = new Date("June 21 2012 11:59 PM");
    var Tennis_2_Start = new Date("June 24 2012 12:01 AM");
    var Tennis_2_Stop = new Date("June 28 2012 11:59 PM");
    var Volleyball_Start = new Date("July 8 2012 12:01 AM");
    var Volleyball_Stop = new Date("July 11 2012 11:59 PM");
    var Soccer_1_Start = new Date("July 8 2012 12:01 AM");
    var Soccer_1_Stop = new Date("July 12 2012 11:59 PM");
    var IACAC_Start = new Date("July 9 2012 12:01 AM");
    var IACAC_Stop = new Date("July 11 2012 11:59 PM");
    var Summer_Forensics_Start = new Date("July 15 2012 12:01 AM");
    var Summer_Forensics_Stop = new Date("July 28 2012 11:59 PM");
    var Soccer_2_Start = new Date("July 22 2012 12:01 AM");
    var Soccer_2_Stop = new Date("July 26 2012 11:59 PM");
    var Cross_Country_Start = new Date("July 25 2012 12:01 AM");
    var Cross_Country_Stop = new Date("July 28 2012 11:59 PM");

    if((today <= Traffic_Court_Stop && today >= Traffic_Court_Start))
        {
            document.getElementById( traffic_court ).classList.add( active );
        };

    if((today <= Summer_Enrichment_Stop && today >= Summer_Enrichment_Start))
        {
            document.getElementById( summer_enrichment ).classList.add( active );
        };

    if((today <= Wbb_Ind_Stop && today >= Wbb_Ind_Start))
        {
            document.getElementById( wbb_ind ).classList.add( active );
        };

    if((today <= Soccer_Referee_Stop && today >= Soccer_Referee_Start))
        {
            document.getElementById( soccer_referee ).classList.add( active );
        };

    if((today <= Broadcast_Stop && today >= Broadcast_Start))
        {
            document.getElementById( broadcast ).classList.add( active );
        };

    if((today <= Tennis_1_Stop && today >= Tennis_1_Start))
        {
            document.getElementById( tennis_1 ).classList.add( active );
        };

    if((today <= Tennis_2_Stop && today >= Tennis_2_Start))
        {
            document.getElementById( tennis_2 ).classList.add( active );
        };

    if((today <= Volleyball_Stop && today >= Volleyball_Start))
        {
            document.getElementById( volleyball ).classList.add( active );
        };

    if((today <= Soccer_1_Stop && today >= Soccer_1_Start))
        {
            document.getElementById( soccer_1 ).classList.add( active );
        };

    if((today <= IACAC_Stop && today >= IACAC_Start))
        {
            document.getElementById( IACAC ).classList.add( active );
        };

    if((today <= Summer_Forensics_Stop && today >= Summer_Forensics_Start))
        {
            document.getElementById( summer_forensics ).classList.add( active );
        };

    if((today <= Soccer_2_Stop && today >= Soccer_2_Start))
        {
            document.getElementById( soccer_2 ).classList.add( active );
        };

    if((today <= Cross_Country_Stop && today >= Cross_Country_Start))
        {
            document.getElementById( cross_country ).classList.add( active );
        };
});
</script>

As you can see, I start by getting today s date, then I specify the start and end dates for each of the camps. Then, I use a bunch of if-statements to determine if today is in between the start and end dates for each camp. If that is true, it adds the class "active" to the row corresponding to that camp.

看来对头两个营地的工作是正确的,即如果我把今天的价值改变到5月31日,就会突出第一行。 或者,如果我改为6月15日,就会突出第二行。 问题在于,如果今天是6月15日,它就应当在今年夏天强调BOTH,即富足和足球。 这只突出了夏季的富集。

I ve double-checked the element IDs that I reference, the spelling of my variables, etc. and everything seems to look correct (unless I missed something obvious). Is there any reason why I can t use the script as I ve presented it? I don t think this is the case, but is there a problem with using so many if-statements in a row like that? My only thought is that perhaps it thinks they are a bunch of else-cases but I thought I d escape that with the semi-colon after each if-statement.

任何想法? 或许是这样做的更好途径? 我对一些答复进行仔细研究,但似乎没有发现其他任何地方的类似情况(尽管我确信,在得到更好的执行之前已经这样做)。

THANKS FOR YOUR HELP!!!

EDIT:在得到其中一些答复的帮助之后,我对未来可能 st倒这一问题的使用者持有最后守则。

<script type="text/javascript">
$(document).ready(function() {
    var today = new Date();

    var SCHEDULE = {
         tc : [ May 31 2012 ,  June 1 2012 ],
         se : [ June 10 2012 ,  June 16 2012 ],
         wbb : [ June 11 2012 ,  June 14 2012 ],
         sr : [ June 15 2012 ,  June 16 2012 ],
         broadcast : [ June 17 2012 ,  June 21 2012 ],
         ten1 : [ June 17 2012 ,  June 21 2012 ],
         ten2 : [ June 24 2012 ,  June 28 2012 ],
         volleyball : [ July 8 2012 ,  July 11 2012 ],
         soc1 : [ July 8 2012 ,  July 12 2012 ],
         iacac : [ July 9 2012 ,  July 11 2012 ],
         sf : [ July 15 2012 ,  July 28 2012 ],
         soc2 : [ July 22 2012 ,  July 26 2012 ],
         cc : [ July 25 2012 ,  July 28 2012 ]
    };

    for (var camp in SCHEDULE) {
    console.log( checking   + camp +  , dates have to be within the   + SCHEDULE[camp] +   range );
    //console.log(Date.parse(SCHEDULE[camp][0]));
        if (today >= Date.parse(SCHEDULE[camp][0]) && today <= Date.parse(SCHEDULE[camp][1])) {
            console.log(camp +   is currently in session! );
            document.getElementById(camp).classList.add( active ); 
        };
    };
});
</script>

THANKS A LOT EVERYONE!!!

最佳回答

我建议,在服务器方面采用逻辑,这样,不光彩的内容不会“闪电”加业绩稍微改善。 值得论证的更重要的改进是,不论客户的时间环境如何,始终如一。

虽然Istrongly鼓吹上述原则,但《联合材料》的解决办法也可成为更可维持的办法。 1. 编制你的法典,以包含一个可变的功能,对照范围核对日期,并采用一个类别,使参考资料的保存方式正常化:

var TODAY = new Date();

//Individual dates could be organized as objects,
//but using arrays below seems to be more readable and tidy
var SCHEDULE = {
     some-class : [ May 5 2011 ,  June 5 2011 ],
     some-class-a : [ May 5 2012 ,  June 5 2012 ],
     some-class-b : [ May 10 2012 ,  June 10 2012 ],
     some-class-c : [ May 15 2012 ,  June 15 2012 ],
     some-class-d : [ May 20 2012 ,  June 20 2012 ],
     some-class-e : [ May 25 2012 ,  June 25 2012 ]
};

for (var camp_ in SCHEDULE) {
    if (TODAY >= Date.parse(SCHEDULE[camp_][0]) && TODAY <= Date.parse(SCHEDULE[camp_][1])) {
        document.getElementById(camp_).classList.add( active );
    }
}

http://jsfiddle.net/ovfiddle/bMBcq/“rel=“nofollow”http://jsfiddle.net/ovfiddle/bMBcq/

问题回答

Here s an attempt. Since you re not using a database, try storing your dates as data attributes. Convert them to timestamps so you can compare: http://jsfiddle.net/uJEJ7/26/

我确信我对一些东西视而不见,但也许会引发某种想法。

我注意到你的网页是用购买力平价书写的,走这条路可能比较容易。 a. 将营地数据储存在相关的阵列中,通过它来排出表格,并根据您的日期进行产出标记。

Good luck!

我不敢肯定,你能否在日期上使用更多的/少于操作员,但比较他们的时间印章肯定会奏效,因为你将同人数(百万秒)合作。

你们可以找到这样的时间:

var today = new Date().getTime();




相关问题
Mysql compaire two dates from datetime?

I was try to measure what is faster and what should be the best way to compare two dates, from datetime record in MySql db. There are several approaches how to grab a date from date time, but I was ...

iPhone Date Picker rolls over to 2010 on December 27th?

So I implemented a UIDatepicker in one of my applications for scheduling and autodialing teleconferences... everything is pretty much ready to go except, while testing I noticed that when the date ...

Convert date Python

I have MMDDYY dates, i.e. today is 111609 How do I convert this to 11/16/2009, in Python?

specifying date format when using $form->inputs() in CakePHP

I am wondering if there is a way to specify the date format in the forms created using CakePHP s $form->inputs(); Please note that this is not the individual $form->input() but instead $form->inputs() ...

NSDateFormat, super simple! Where am I screwing up?

Ok, this is really simple, maybe I m a getting a bit burnt out, but seems like it should work, Query XML feed, put out date string, format, display in a cell. The issue is I m a getting a NULL ...

sqlite writing a date into an email

I am exporting a date value from sqlite and placing it into an email. The date appears like this 279498721.322872 I am using Objective C in an Iphone App. Does anyone know how to make this export ...