moment-countdown js library给了我一个奇怪的变量赋值?

时间:2018-04-30 04:12:00

标签: javascript html html5 momentjs countdownjs.js

我正在使用的图书馆:https://github.com/icambron/moment-countdown

我的代码:

<!DOCTYPE html>
<html>

<head>
  <title>Test</title>
  <!-- https://github.com/icambron/moment-countdown -->
  <script src="moment.min.js"></script>
  <script src="countdown.min.js"></script>
  <script src="moment-countdown.min.js"></script>
</head>

<body>
  <input type="datetime-local" id="timeInputHTML" onchange="mainfunc()>
  <p id="pel"></p>
  <script>
    function mainfunc() {
      timeVar = document.getElementById("timeInputHTML").value;
      // var timeInputVar = moment("2045-01-01 00:00:00").countdown().toString(); 
      var timeInputVar = moment(timeVar).countdown().toString();
      document.getElementById("pel").innerHTML = timeInputVar;
    }

    // Run above function every 1 second
    setInterval(mainfunc, 1000);
  </script>
</body>

</html>

问题:如果变量timeVar在HTML代码中从timeInputHTML获取值,而不包含中间的T,那么我如何制作变量"2018-05-05T14:30"Uncaught TypeError我相信这是我的代码出了什么问题,并导致php artisan make:command RedisDataListener如果它不是问题那么请你能解释一下是什么问题吗?

图片/截图:

Screenshot

1 个答案:

答案 0 :(得分:0)

cmdPrint

你在输入函数中缺少&#34; 这应该是这样的

<!DOCTYPE html>
<html>

<head>
  <title>Test</title>
  <!-- https://github.com/icambron/moment-countdown -->
  <script src="moment.min.js"></script>
  <script src="countdown.min.js"></script>
  <script src="moment-countdown.min.js"></script>
</head>

<body>
  <input type="datetime-local" id="timeInputHTML" onchange="mainfunc()">
  <p id="pel"></p>
  <script>
    function mainfunc() {
      timeVar = document.getElementById("timeInputHTML").value;
      // var timeInputVar = moment("2045-01-01 00:00:00").countdown().toString(); 
      var timeInputVar = moment(timeVar).countdown().toString();
      document.getElementById("pel").innerHTML = timeInputVar;
    }

    // Run above function every 1 second
    setInterval(mainfunc, 1000);
  </script>
</body>

</html>