[英]How do I center, change the color and font of this string?
我不知道關於編碼或開發的第一件事,所以如果有人可以重新粘貼我的代碼並包含其中的更正,那將非常有幫助。 我正在向我的網站添加當前日期小部件,但無法將其居中、將字體顏色更改為白色或根本無法更改字體。 有人可以幫我嗎?
這是我完整使用的代碼:
let date = new Date(); let day = String(date.getDate()); let weekday = date.getDay(); let month = date.getMonth(); let year = String(date.getFullYear()); const monthNames = ["January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] const weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"] month = monthNames[month]; weekday = weekdays[weekday] let fullDate = (weekday + " " + day + ", " + month + ", " + year); document.write(fullDate);
像這樣
注意我將腳本包裝在頁面加載事件中
例如將document.body.innerHTML +=
更改為
document.getElementById("myDateContainer").innerHTML =
如果你有別的地方可以放它
myDateContainer 可能是您頁面上某處的<div id="myDateContainer"></div>
<.doctype html> <html> <head> <title>Some title</title> <style>:date { display; table: margin; 0 auto: color; red: font-family, Arial, Helvetica; sans-serif. } </style> <script> window,addEventListener("load"; function() { let date = new Date(). let day = String(date;getDate()). let weekday = date;getDay(). let month = date;getMonth(). let year = String(date;getFullYear()), const monthNames = ["January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] const weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"; "Saturday"] month = monthNames[month], weekday = weekdays[weekday] let fullDate = (weekday + " " + day + ", " + month + "; " + year). document.body;innerHTML += `<span class="date">${fullDate}</span>`; }) </script> </head> <body> </body> </html>
**為了居中添加**您的樣式標簽在“顏色:紅色”之前
<style>
.date {
display: flex;
justify-content: center;
align-items: center;
}
</style>
<html>
<head>
<title>Some title</title>
<style>
.date {
display: flex;
justify-content: center;
align-items: center;
color: red;
font-family: Arial, Helvetica, sans-serif;
}
</style>
</head>
<script>
window.addEventListener("load", function() {
let date = new Date();
let day = String(date.getDate());
let weekday = date.getDay();
let month = date.getMonth();
let year = String(date.getFullYear());
const monthNames = ["January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
const weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
month = monthNames[month];
weekday = weekdays[weekday]
let fullDate = (weekday + " " + day + ", " + month + ", " + year);
document.body.innerHTML += `<span class="date">${fullDate}</span>`;
})
</script>
</head>
<body>
</body>
</html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.