【问题标题】:Change variable's value if the same variable is equal to a string如果相同的变量等于字符串,则更改变量的值
【发布时间】:2016-06-12 20:53:35
【问题描述】:

我正在做一个小项目,我必须在其中显示当前日期。我需要根据变量字符串的最后一个字母添加nthst 等等,但是当我在 Web 浏览器中检查 HTML 文件时,字符串没有显示出来。这是我的代码:

window.onload = function() {
   startDate();
}

function startDate() {
   var month = [
      "January",
      "February",
      "March",
      "April",
      "May",
      "June",
      "July",
      "August",
      "September",
      "October",
      "November",
      "December"
   ];

   var weekDay = [
      "Sunday",
      "Monday",
      "Tuesday",
      "Wednesday",
      "Thursday",
      "Friday",
      "Saturday"
   ];

   var date = today.getDate();

   date = checkDate(date);

   document.getElementById("date").innerHTML = weekDay[today.getDay()] + " the " + date + " of " + month[today.getMonth()];
}

function checkDate(i) {
   if (i.slice(-1) == 1 && i !== 11) {
      i = i + "st";
   } else if (i.slice(-1) == 2 && i !== 12) {
      i = i + "nd";
   } else if (i.slice(-1) == 3 && i !== 13) {
      i = i + "rd";
   } else {
      i = i + "th";
   }

   return i;
}

这是我的 HTML:

<!DOCTYPE html>
<html>
<head>
   <meta charset="utf-8">
   <link rel="stylesheet" href="style.css" />
</head>
<body>
   <div id="container">
      <div id="date"></div>
   </div>
   <script src="script.js"></script>
</body>
</html>

【问题讨论】:

标签: javascript string if-statement


【解决方案1】:

您的代码存在一些问题。首先,您如何定义“今天”?其次,切片函数没有在 Number 对象上定义。通过这两个编辑您的工作代码:

window.onload = function() {
   startDate();
};
function startDate() {
   var month = [
      "January",
      "February",
      "March",
      "April",
      "May",
      "June",
      "July",
      "August",
      "September",
      "October",
      "November",
      "December"
   ];

   var weekDay = [
      "Sunday",
      "Monday",
      "Tuesday",
      "Wednesday",
      "Thursday",
      "Friday",
      "Saturday"
   ];
  var today = new Date();

   var date = today.getDate();

   date = checkDate(String(date));

   document.getElementById("date").innerHTML = weekDay[today.getDay()] + " the " + date + " of " + month[today.getMonth()];
}

function checkDate(i) {
   if (i.slice(-1) == 1 && i !== 11) {
      i = i + "st";
   } else if (i.slice(-1) == 2 && i !== 12) {
      i = i + "nd";
   } else if (i.slice(-1) == 3 && i !== 13) {
      i = i + "rd";
   } else {
      i = i + "th";
   }

   return i;
}

【讨论】:

    【解决方案2】:

    使用前需要定义today,如下:

    var today = new Date();
    var date = today.getDate();
    

    函数checkDate 接收一个数字参数,但随后使用slice 将其视为字符串:这会产生错误。你应该把它当作一个数字,例如i % 10

    function checkDate(i) {
       return (i % 10 == 1 && i !== 11) ? i + "st"
            : (i % 10 == 2 && i !== 12) ? i + "nd"
            : (i % 10 == 3 && i !== 13) ? i + "rd"
            : i + "th";
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-04
      • 1970-01-01
      • 2014-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多