在HTML中显示的javascript日期不起作用

问题描述 投票:-2回答:1

我已经处理过一个简单的JS / HTML工作,我在使用document.body.innerHTML在JS中显示一个html代码时出现问题,这里是代码:

plz help,tha

let html;

let rday;
rday = new Date();

let day;
day = rday.getDay();

let date;
date = rday.getDate();

let month;
month = rday.getMonth();

let year;
year = rday.getFULLYear();

html =`
<P> <span> ${day} </span>
<span> ${month}/ </span>
<span> ${date}/ </span>
<span> ${year} </span> </p>
`;
console.log(html);
document.body.innerHTML = hmtl;
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <script type="text/javascript" src="javascript.js">

    </script>
  </body>
</html>

你可以看到我尝试了控制台,但问题是什么?这是pic:enter image description here

javascript html5
1个回答
0
投票

您的代码中存在一些语法错误。

  1. 你打电话给.getFULLYear()这是不正确的,应该是.getFullYear() https://www.w3schools.com/jsref/jsref_getfullyear.asp
  2. 你拼错了html

let html;

let rday;
rday = new Date();

let day;
day = rday.getDay();

let date;
date = rday.getDate();

let month;
month = rday.getMonth();

let year;
year = rday.getFullYear(); // <- incorrect function call

html =`
<P> <span> ${day} </span>
<span> ${month}/ </span>
<span> ${date}/ </span>
<span> ${year} </span> </p>
`;
console.log(html);
document.body.innerHTML = html; // <- you spelt html wrong
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <script type="text/javascript" src="javascript.js">

    </script>
  </body>
</html>
© www.soinside.com 2019 - 2024. All rights reserved.