将 JavaScript 输出打印为 HTML

问题描述 投票:0回答:1

第一次在这里发帖,请多多包涵!我正在学习 JavaScript 课程,我们正在制作一个网页,该网页会随着 Unix 纪元以来的时间而更新。这包括毫秒、秒、分钟、小时、天和年。我很难将 JavaScript 输出到 HTML 页面。

这是我的 HTML:

<body>
    <h1>Welcome to my Time Calculator</h1>
    <main>
        <p>Below is the time that has elapsed since the
            Unix Epoch: 1/1/1970 at midnight. Hit refresh
            to update the calculation.</p>
        <div>
            <label>Milliseconds</label>
            <span id="millis"></span>
        </div>
        <div>
            <label>Seconds</label>
            <span id="seconds"></span>
        </div>
        <div>
            <label>Minutes</label>
            <span id="minutes"></span>
        </div>
        <div>
            <label>Hours</label>
            <span id="hours"></span>
        </div>
        <div>
            <label>Days</label>
            <span id="days"></span>
        </div>
        <div>
            <label>Years</label>
            <span id="years"></span>
        </div>
    </main>
    <script src="scripts/time.js"></script>
</body>`

这是我的JS:

let millis = new Date().getTime;
let millisInSeconds = millis / 1000;
let secondsInMinutes =  millisInSeconds * 60;
let minutesInHours = secondsInMinutes * 60;
let hoursInDays = minutesInHours * 24;
let daysInYears = hoursInDays * 365;

millis = parseFloat(millis);
millisInSeconds = parseFloat(millisInSeconds);
secondsInMinutes = parseFloat(secondsInMinutes);
minutesInHours = parseFloat(minutesInHours);
hoursInDays = parseFloat(hoursInDays);
daysInYears = parseFloat(daysInYears);

const millisDoc = document.querySelector("#millis").innerHTML = millis;
const secondsDoc = document.querySelector("#seconds").innerHTML = millisInSeconds;
const minutesDoc = document.querySelector("#minutes").innerHTML = secondsInMinutes;
const hoursDoc = document.querySelector("#hours").innerHTML = minutesInHours;
const daysDoc = document.querySelector("#days").innerHTML = hoursInDays;
const yearsDoc = document.querySelector("#years").innerHTML = daysInYears;
javascript html dom output
1个回答
0
投票

你的逻辑有几个问题:

  • getTime
    是一种方法,因此您需要包含
    ()
    来调用它:
    new Date().getTime()
  • 在这种情况下,
    const xDoc =
    分配是多余的,应该删除。
  • 秒、分钟、小时和天值是通过除以前一个值而不是相乘得出的。

这是所做的更正的工作示例:

let millis = new Date().getTime();
let millisInSeconds = millis / 1000;
let secondsInMinutes = millisInSeconds / 60;
let minutesInHours = secondsInMinutes / 60;
let hoursInDays = minutesInHours / 24;
let daysInYears = hoursInDays / 365;

millis = parseFloat(millis);
millisInSeconds = parseFloat(millisInSeconds);
secondsInMinutes = parseFloat(secondsInMinutes);
minutesInHours = parseFloat(minutesInHours);
hoursInDays = parseFloat(hoursInDays);
daysInYears = parseFloat(daysInYears);

document.querySelector("#millis").innerHTML = millis;
document.querySelector("#seconds").innerHTML = millisInSeconds;
document.querySelector("#minutes").innerHTML = secondsInMinutes;
document.querySelector("#hours").innerHTML = minutesInHours;
document.querySelector("#days").innerHTML = hoursInDays;
document.querySelector("#years").innerHTML = daysInYears;
<main>
  <p>Below is the time that has elapsed since the Unix Epoch: 1/1/1970 at midnight. Hit refresh to update the calculation.</p>
  <div>
    <label>Milliseconds</label>
    <span id="millis"></span>
  </div>
  <div>
    <label>Seconds</label>
    <span id="seconds"></span>
  </div>
  <div>
    <label>Minutes</label>
    <span id="minutes"></span>
  </div>
  <div>
    <label>Hours</label>
    <span id="hours"></span>
  </div>
  <div>
    <label>Days</label>
    <span id="days"></span>
  </div>
  <div>
    <label>Years</label>
    <span id="years"></span>
  </div>
</main>

© www.soinside.com 2019 - 2024. All rights reserved.