我想动态生成基于当前一天的文本字符串。所以,例如,如果它是第1天,那么我想我的代码生成=“它的<动态>1*<动态字符串>st</动态字符串>*</动态>”。

总共有12天,所以我做了以下事情:

我设置了一个for循环,循环12天。 在我的html中,我已经给了我的元素一个唯一的id来瞄准它,如下所示: <h1 id="dynamicTitle" class="CustomFont leftHeading shadow">On The <span></span> <em>of rest of generic text</em></h1> 然后,在我的for循环中,我有以下代码: $ (" # dynamicTitle跨度”). html(我); Var day = i; If (day == 1) { Day = I +“st”; } else if (day == 2) { 日= I + "nd" } else if (day == 3) { Day = I + "rd" }

更新

这是请求的整个for循环:

$(document).ready(function () {
    for (i = 1; i <= 12; i++) {
        var classy = "";
        if (daysTilDate(i + 19) > 0) {
            classy = "future";
            $("#Day" + i).addClass(classy);
            $("#mainHeading").html("");
            $("#title").html("");
            $("#description").html("");
        } else if (daysTilDate(i + 19) < 0) {
            classy = "past";
            $("#Day" + i).addClass(classy);
            $("#title").html("");
            $("#description").html("");
            $("#mainHeading").html("");
            $(".cta").css('display', 'none');
            $("#Day" + i + " .prizeLink").attr("href", "" + i + ".html");
        } else {
            classy = "current";
            $("#Day" + i).addClass(classy);
            $("#title").html(headings[i - 1]);
            $("#description").html(descriptions[i - 1]);
            $(".cta").css('display', 'block');
            $("#dynamicImage").attr("src", ".." + i + ".jpg");
            $("#mainHeading").html("");
            $(".claimPrize").attr("href", "" + i + ".html");
            $("#dynamicTitle span").html(i);
            var day = i;
            if (day == 1) {
                day = i + "st";
            } else if (day == 2) {
                day = i + "nd"
            } else if (day == 3) {
                day = i + "rd"
            } else if (day) {
            }
        }
    }

当前回答

function getSuffix(n) {return n < 11 || n > 13 ? ['st', 'nd', 'rd', 'th'][Math.min((n - 1) % 10, 3)] : 'th'}

其他回答

我写了这个函数来解决这个问题:

// this is for adding the ordinal suffix, turning 1, 2 and 3 into 1st, 2nd and 3rd
Number.prototype.addSuffix=function(){
    var n=this.toString().split('.')[0];
    var lastDigits=n.substring(n.length-2);
    //add exception just for 11, 12 and 13
    if(lastDigits==='11' || lastDigits==='12' || lastDigits==='13'){
        return this+'th';
    }
    switch(n.substring(n.length-1)){
        case '1': return this+'st';
        case '2': return this+'nd';
        case '3': return this+'rd';
        default : return this+'th';
    }
};

有了这个,你可以把. addsuffix()放在任何数字上,它就会得到你想要的结果。例如:

var number=1234;
console.log(number.addSuffix());
// console will show: 1234th

通过将数字分成一个数组并反转,我们可以很容易地使用数组[0]和数组[1]检查数字的最后两位数字。

如果一个数字是十位数数组[1]= 1,它需要“th”。

function getDaySuffix(num)
{
    var array = ("" + num).split("").reverse(); // E.g. 123 = array("3","2","1")
    
    if (array[1] != "1") { // Number is not in the teens
        switch (array[0]) {
            case "1": return "st";
            case "2": return "nd";
            case "3": return "rd";
        }
    }
    
    return "th";
}

你只有12天?我想让它只是一个简单的查找数组:

var suffixes = ['','st','nd','rd','th','th','th','th','th','th','th','th','th'];

然后

var i = 2;
var day = i + suffixes[i]; // result: '2nd'

or

var i = 8;
var day = i + suffixes[i]; // result: '8th'

<p>31<sup>st</sup> 2015年3月</p>

你可以使用

圣1 < sup > < / sup > 2 < sup >和< / sup > 研发3 < sup > < / sup > 4 < sup >的< / sup >

用于定位后缀

我强烈推荐这本书,它超级简单易懂。希望有帮助?

它避免使用负整数,即小于1的数字并返回false 如果输入为0,则返回0

function numberToOrdinal(n) {

  let result;

  if(n < 0){
    return false;
  }else if(n === 0){
    result = "0";
  }else if(n > 0){

    let nToString = n.toString();
    let lastStringIndex = nToString.length-1;
    let lastStringElement = nToString[lastStringIndex];

    if( lastStringElement == "1" && n % 100 !== 11 ){
      result = nToString + "st";
    }else if( lastStringElement == "2" && n % 100 !== 12 ){
      result = nToString + "nd";
    }else if( lastStringElement == "3" && n % 100 !== 13 ){
      result = nToString + "rd";
    }else{
      result = nToString + "th";
    }

  }

  return result;
}

console.log(numberToOrdinal(-111));
console.log(numberToOrdinal(0));
console.log(numberToOrdinal(11));
console.log(numberToOrdinal(15));
console.log(numberToOrdinal(21));
console.log(numberToOrdinal(32));
console.log(numberToOrdinal(43));
console.log(numberToOrdinal(70));
console.log(numberToOrdinal(111));
console.log(numberToOrdinal(300));
console.log(numberToOrdinal(101));

输出

false
0
11th
15th
21st
32nd
43rd
70th
111th
300th
101st