You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何借助Bootstrap与HTML将指定日期JavaScript代码实现为脚注?

Hey there! Let's break down how to turn your date-displaying JavaScript into a clean Bootstrap-styled footer (footnote) and fix up that code to follow best practices.

Step 1: Ditch the Outdated document.write

First off, let's move away from document.write—it's not ideal for inserting content after the page loads, and it can mess up your layout. Instead, we'll use DOM manipulation to inject the dates directly into a polished Bootstrap footer component.

Step 2: Full Working Implementation

Here's the complete code that meets your teacher's requirements, with proper Bootstrap styling and safe JS practices:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <!-- Bootstrap CSS for styling -->
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
  <!-- Moment.js (required for date parsing/formatting) -->
  <script src="https://cdn.jsdelivr.net/npm/moment@2.29.4/moment.min.js"></script>
  <title>Bootstrap Footer Date Display</title>
</head>
<!-- Flex classes to push footer to page bottom -->
<body class="d-flex flex-column min-vh-100">
  <!-- Your main page content goes here -->
  <div class="container mt-5">
    <h1>Your Page Content</h1>
    <p>This is where your main website content lives.</p>
  </div>

  <!-- Bootstrap-styled Footer (our "footnote") -->
  <footer class="footer mt-auto py-3 bg-light">
    <div class="container">
      <!-- Empty span to inject date content into -->
      <span id="date-footer-text" class="text-muted"></span>
    </div>
  </footer>

  <!-- JavaScript to calculate and display dates -->
  <script>
    // Wait for page to fully load before running code
    document.addEventListener('DOMContentLoaded', function() {
      // Parse last modified date with Moment.js
      const lastModified = moment(document.lastModified, "MM/DD/YYYY");
      // Get current date
      const currentDate = moment();
      // Target the footer element
      const footerTextElement = document.getElementById('date-footer-text');

      // Insert formatted dates into the footer
      footerTextElement.textContent = `The last modified date is: ${lastModified.format("YYYY-MM-DD")} and the current date is: ${currentDate.format("YYYY-MM-DD")}`;
    });
  </script>
</body>
</html>

Step 3: Key Details Explained

Let's unpack what makes this work well:

  • Bootstrap Footer Styling: The footer uses classes like mt-auto (pushes it to the bottom of the page), py-3 (adds vertical padding), bg-light (soft background), and text-muted (subtle text color) to fit Bootstrap's design system.
  • Safe DOM Injection: We use textContent to insert the date text instead of innerHTML—this avoids potential security risks like XSS attacks.
  • Page Load Safety: The DOMContentLoaded event ensures our JS only runs after all HTML elements are loaded, so we never try to access the footer before it exists.
  • Fixed Footer Option: The body tag's d-flex flex-column min-vh-100 classes make the footer stay anchored to the bottom even if your page has little content. Remove these if you want the footer to follow your content normally.

Step 4: General Pattern for JS → Bootstrap Footnotes

If you need to do this for other JS content later, follow this simple pattern:

  1. Create a Bootstrap-styled footer (or inline footnote) with an empty HTML element (like a <span> or <div>) and give it a unique id.
  2. Modify your JavaScript to target that element using getElementById or querySelector.
  3. Use textContent or innerHTML (only if you trust the content!) to inject your JS-generated content into the element.
  4. Tweak styling with Bootstrap's utility classes to match your design.

内容的提问来源于stack exchange,提问作者Floriane Shala

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:31:40