我已经完成了一个简单的JS / HTML工作,并且在使用 document.body.innerHTML 在JS中显示html代码时遇到问题,这是代码:
plz帮助, 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>
如您所见,我尝试过控制台,但是出了什么问题? 这是图片: enter image description here
答案 0 :(得分:0)
您的代码中有一些语法错误。
.getFULLYear()
,这是不正确的,应该是.getFullYear()
https://www.w3schools.com/jsref/jsref_getfullyear.asp 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>