gpt4 book ai didi

Javascript 和 CSS 代码字面上出现在我的 HTML 页面上

转载 作者:行者123 更新时间:2023-12-01 23:30:52 25 4
gpt4 key购买 nike

这是一个简单的日历代码。我的 javascript 和 CSS 代码确实出现在我的日历旁边。有谁知道为什么会发生这样的事情?我正在使用在线 html/css/js 编辑器,当我将代码制作成 HTML 文件时,发生了这种情况。我花了几个小时寻找故障,但找不到任何有问题的地方。

<!DOCTYPE html>

<html lang="en">

<head>
<meta charset="utf-8" />
<title>ICSI301 - Lab 2</title>
</head>

<body>
<h1 id="year">2021 School Calendar</h1>
<div class="calendar">
</div>
</body>

</html>
<script>

var monthNamesRy = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var daysOfTheWeekRy = ["S", "M", "T", "W", "T", "F", "S"]

var d = new Date();
var year = d.getFullYear();

var thisMonth = d.getMonth();
var today = d.getDate();
//var nthday = d.getDay();
var daysOfTheMonthDiv = document.querySelectorAll(".daysOfTheMonth");

for (var month = 0; month < 12; month++) {
createCalendar(month);
}

function createCalendar(month) {
var monthDiv = createMonthHeader(month);

var firstDayOfTheMonth = getFirstDayOfTheMonth(year, month);
var daysinmonth = daysInMonth(year, month)
var counter = 0, order = 6;

for (var i = 0; i < firstDayOfTheMonth + 7; i++) {
order++;
createDay(month, "&nbsp;", order, monthDiv);
}
for (var i = firstDayOfTheMonth; i < daysInMonth(year, month) + firstDayOfTheMonth; i++) {
counter++;
order++;
createDay(month, counter, order, monthDiv);
}

for (var i = firstDayOfTheMonth + daysinmonth; i < 6 * 7; i++) {
order++;
createDay(month, "&nbsp;", order, monthDiv);
}
}

function createDay(month, counter, order, monthDiv) {

var day = document.createElement("div");
if (month == thisMonth && counter == today) {
day.setAttribute("class", "to day");
} else {
day.setAttribute("class", "day");
}
day.setAttribute("style", "order:" + order);
day.innerHTML = counter;
monthDiv.appendChild(day);
}

function createMonthHeader(month) {
var calendar = document.querySelector(".calendar");

var monthDiv = document.createElement("div");
monthDiv.setAttribute("class", "month");
calendar.appendChild(monthDiv);

var h4 = document.createElement("h4");
h4.innerHTML = monthNamesRy[month];
monthDiv.appendChild(h4);

for (var i = 0; i < 7; i++) {
var hday = document.createElement("div");
hday.setAttribute("class", "day OfWeek");
hday.setAttribute("style", "order:" + i);
hday.innerHTML = daysOfTheWeekRy[i].toUpperCase();
monthDiv.appendChild(hday);
}

return monthDiv;
}

function daysInMonth(year, month) {
return new Date(year, month + 1, 0).getDate();
}

function getMonthName(month) {
return monthNamesRy[month];
}
function getDayName(day) {
return daysOfTheWeekRy[day];
}

function getFirstDayOfTheMonth(y, m) {
var firstDay = new Date(y, m, 1);
return firstDay.getDay();
}
function getLastDayOfTheMonth(y, m) {
var lastDay = new Date(y, m + 1, 0);
return lastDay.getDay();
}
</script>
<style>

body * {
margin: 0;
padding: 0;
font-family: "Times New Roman";
}

.calendar, section {
max-width: 50rem;
}

.day {
width: 1.5em;
height: 1.5em;
}

.day:nth-of-type(-n+7) {
background-color: #7CFC00;
}

.to.day {
background: aquamarine;
}

.month {
width: calc(1.5em * 8);
padding: 1em;
}

h4 {
font-size: 1em;
text-transform: uppercase;
}

h1#year {
font-size: 3em;
height: 29px;
font-weight: normal;
padding: 1em 1em .5em 1em;
margin-bottom: .5em;
color: #006400;
}

body, body * {
display: flex;
justify-content: center;
}

h4 {
justify-content: center;
flex: 1 0 100%;
}

h1 {
justify-content: center;
align-self: stretch;
}

.calendar, .month {
flex-wrap: wrap;
}

.month {
align-items: flex-start;
border: 3px double black;
margin: 5px;
}

.day {
border: 1px solid black;
align-items: center;
justify-content: center;
}

</style>

最佳答案

  1. 将脚本和样式移到头部
  2. 改变这个
    var daysOfTheMonthDiv = document.querySelectorAll(".daysOfTheMonth");

for (var month = 0; month < 12; month++) {
createCalendar(month);
}

    window.addEventListener("load",function() { // wait for page load
var daysOfTheMonthDiv = document.querySelectorAll(".daysOfTheMonth");
for (var month = 0; month < 12; month++) {
createCalendar(month);
}
})

作为Chris noticed ,你有一个非常颠覆性的风格条目

body,
body * {
display: flex;
justify-content: center;
}

我把它移到了另一种体型的正下方,现在必须添加

script,
style {
display: none
}

停止干扰

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8" />
<title>ICSI301 - Lab 2</title>
<script>
var monthNamesRy = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var daysOfTheWeekRy = ["S", "M", "T", "W", "T", "F", "S"]

var d = new Date();
var year = d.getFullYear();

var thisMonth = d.getMonth();
var today = d.getDate();
//var nthday = d.getDay();
window.addEventListener("load", function() {
var daysOfTheMonthDiv = document.querySelectorAll(".daysOfTheMonth");

for (var month = 0; month < 12; month++) {
createCalendar(month);
}
});

function createCalendar(month) {
var monthDiv = createMonthHeader(month);

var firstDayOfTheMonth = getFirstDayOfTheMonth(year, month);
var daysinmonth = daysInMonth(year, month)
var counter = 0,
order = 6;

for (var i = 0; i < firstDayOfTheMonth + 7; i++) {
order++;
createDay(month, "&nbsp;", order, monthDiv);
}
for (var i = firstDayOfTheMonth; i < daysInMonth(year, month) + firstDayOfTheMonth; i++) {
counter++;
order++;
createDay(month, counter, order, monthDiv);
}

for (var i = firstDayOfTheMonth + daysinmonth; i < 6 * 7; i++) {
order++;
createDay(month, "&nbsp;", order, monthDiv);
}
}

function createDay(month, counter, order, monthDiv) {

var day = document.createElement("div");
if (month == thisMonth && counter == today) {
day.setAttribute("class", "to day");
} else {
day.setAttribute("class", "day");
}
day.setAttribute("style", "order:" + order);
day.innerHTML = counter;
monthDiv.appendChild(day);
}

function createMonthHeader(month) {
var calendar = document.querySelector(".calendar");

var monthDiv = document.createElement("div");
monthDiv.setAttribute("class", "month");
calendar.appendChild(monthDiv);

var h4 = document.createElement("h4");
h4.innerHTML = monthNamesRy[month];
monthDiv.appendChild(h4);

for (var i = 0; i < 7; i++) {
var hday = document.createElement("div");
hday.setAttribute("class", "day OfWeek");
hday.setAttribute("style", "order:" + i);
hday.innerHTML = daysOfTheWeekRy[i].toUpperCase();
monthDiv.appendChild(hday);
}

return monthDiv;
}

function daysInMonth(year, month) {
return new Date(year, month + 1, 0).getDate();
}

function getMonthName(month) {
return monthNamesRy[month];
}

function getDayName(day) {
return daysOfTheWeekRy[day];
}

function getFirstDayOfTheMonth(y, m) {
var firstDay = new Date(y, m, 1);
return firstDay.getDay();
}

function getLastDayOfTheMonth(y, m) {
var lastDay = new Date(y, m + 1, 0);
return lastDay.getDay();
}
</script>

<style>
body * {
margin: 0;
padding: 0;
font-family: "Times New Roman";
}

body,
body * {
display: flex;
justify-content: center;
}

script,
style {
display: none
}

.calendar,
section {
max-width: 50rem;
}

.day {
width: 1.5em;
height: 1.5em;
}

.day:nth-of-type(-n+7) {
background-color: #7CFC00;
}

.to.day {
background: aquamarine;
}

.month {
width: calc(1.5em * 8);
padding: 1em;
}

h4 {
font-size: 1em;
text-transform: uppercase;
}

h1#year {
font-size: 3em;
height: 29px;
font-weight: normal;
padding: 1em 1em .5em 1em;
margin-bottom: .5em;
color: #006400;
}

h4 {
justify-content: center;
flex: 1 0 100%;
}

h1 {
justify-content: center;
align-self: stretch;
}

.calendar,
.month {
flex-wrap: wrap;
}

.month {
align-items: flex-start;
border: 3px double black;
margin: 5px;
}

.day {
border: 1px solid black;
align-items: center;
justify-content: center;
}
</style>
</head>

<body>
<h1 id="year">2021 School Calendar</h1>

<div class="calendar"></div>

</body>

</html>

关于Javascript 和 CSS 代码字面上出现在我的 HTML 页面上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66291127/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com