如何借助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
footeruses classes likemt-auto(pushes it to the bottom of the page),py-3(adds vertical padding),bg-light(soft background), andtext-muted(subtle text color) to fit Bootstrap's design system. - Safe DOM Injection: We use
textContentto insert the date text instead ofinnerHTML—this avoids potential security risks like XSS attacks. - Page Load Safety: The
DOMContentLoadedevent 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
bodytag'sd-flex flex-column min-vh-100classes 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:
- Create a Bootstrap-styled footer (or inline footnote) with an empty HTML element (like a
<span>or<div>) and give it a uniqueid. - Modify your JavaScript to target that element using
getElementByIdorquerySelector. - Use
textContentorinnerHTML(only if you trust the content!) to inject your JS-generated content into the element. - Tweak styling with Bootstrap's utility classes to match your design.
内容的提问来源于stack exchange,提问作者Floriane Shala
相关产品推荐
相关产品推荐

