我正在尝试修复this widget (download),因为它说日期是每个月的01TH、02TH和03TH,而我终生找不出它不工作的原因。我没有任何使用JS的经验,但是我对C#很在行,所以它的语法并不难理解。
我意识到这句话才是罪魁祸首:
document.getElementById("monthFC").innerHTML = monthFC + " " + dateFC + "TH";我试着用下面的代码替换它(我知道它在11号、12号和13号是不正确的),但仍然失败:
if (dateFC % 10 == 1)
{
document.getElementById("monthFC").innerHTML = monthFC + " " + dateFC + "ST";
}
else if (dateFC % 10 == 2)
{
document.getElementById("monthFC").innerHTML = monthFC + " " + dateFC + "ND";
}
else if (dateFC % 10 == 3)
{
document.getElementById("monthFC").innerHTML = monthFC + " " + dateFC + "RD";
}
else
{
document.getElementById("monthFC").innerHTML = monthFC + " " + dateFC + "TH";
}据我所知,语法是正确的,所以我只能假设我没有正确地应用模运算符(在C#中,我猜测它将数字存储为字符串,这就是它可能失败的原因,但我不知道这在这里是否适用)。如果有人能为我指明正确的方向,我将不胜感激!提前谢谢。
发布于 2014-12-08 12:51:16
//可以使用switch语句的地方不多了,但这就是其中之一。
function nth(n){
if(n%1) return n;
var n1= n%100;
if(n1>3 && n1<21) return n+'th';
switch(n1%10){
case 1: return n+'st';
case 2: return n+'nd';
case 3: return n+'rd';
default: return n+'th';
}
}
var A= [];
for(var i= 1; i<32; i++)A.push(nth(i));
A.join(', ');//返回值:
1st, 2nd, 3rd, 4th, 5th, 6th, 7th, 8th, 9th, 10th, 11th, 12th, 13th,
14th, 15th, 16th, 17th, 18th, 19th, 20th, 21st, 22nd, 23rd, 24th,
25th, 26th, 27th, 28th, 29th, 30th, 31sthttps://stackoverflow.com/questions/27351409
复制相似问题