JS万年历

 

//style代码

.box{

        width: 200px;

        height: 200px;

        text-align: center;

        background-color: blueviolet;

        float: left;

        margin-left: 20px;

        margin-top: 20px;

        margin-bottom: 20px;

    }

    .box:nth-child(2){

        margin-left: 90px;

    }

    .box:nth-child(8){

        margin-left: 90px;

    }

//JS代码

<script>

        function _calendar(year) {

            var html = '';

            var week = new Date(year, 0).getDay();

            for (let i = 1; i <= 12; i++) {

                html += '<table class="box">';

                html += '<tr><th colspan="7">' + year + '年' + i + '月</th></tr>';

                html += '<tr><td>日</td><td>一</td><td>二</td><td>三</td><td>四</td><td>五</td><td>六</td></tr>';

                html += '<tr>';

                for (let j = 1; j <= new Date(year, i, 0).getDate(); j++) {

                    if (j == 1 && week > 0) {

                        html += '<td colspan="' + week + '"></td>';

                    }

                    html += '<td>' + j + '</td>';

                    if (week == 6) {

                        html += '</tr><tr>'

                    }

                    week = week + 1 > 6 ? 0 : week + 1

                }

                html += '</tr>';

                html += '</table>';

            }

            return html

        }

        var _year = +prompt('请输入正确年份');

        if (!isNaN(_year)) {

            let wannianli = _calendar(_year);

            document.write(wannianli);

        } else {

            alert('请输入正确年份');

        }

    </script>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的 JavaScript 实现万年历代码: ```javascript function isLeapYear(year) { return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0; } function getDaysInMonth(year, month) { if (month === 2) { return isLeapYear(year) ? 29 : 28; } else if ([4, 6, 9, 11].includes(month)) { return 30; } else { return 31; } } function getDayOfWeek(year, month, day) { const days = ["日", "一", "二", "三", "四", "五", "六"]; const date = new Date(year, month - 1, day); return days[date.getDay()]; } function getCalendar(year, month) { const daysInMonth = getDaysInMonth(year, month); const firstDayOfWeek = getDayOfWeek(year, month, 1); const calendar = []; let week = [null, null, null, null, null, null, null]; for (let day = 1, i = 0; day <= daysInMonth; day++, i++) { week[i] = day; if (getDayOfWeek(year, month, day) === "六" || day === daysInMonth) { calendar.push(week); week = [null, null, null, null, null, null, null]; i = -1; } } return { calendar, firstDayOfWeek }; } function formatMonth(month) { return month < 10 ? "0" + month : month; } function createCalendarTable(year, month) { const { calendar, firstDayOfWeek } = getCalendar(year, month); let table = "<table>"; table += "<tr><th>日</th><th>一</th><th>二</th><th>三</th><th>四</th><th>五</th><th>六</th></tr>"; for (let week of calendar) { table += "<tr>"; for (let day of week) { if (day === null) { table += "<td></td>"; } else { table += `<td>${day}</td>`; } } table += "</tr>"; } table += "</table>"; return table; } function createCalendar(year, month) { const { calendar, firstDayOfWeek } = getCalendar(year, month); const monthStr = `${year}年${formatMonth(month)}月`; let calendarHtml = ` <div class="calendar"> <div class="month">${monthStr}</div> ${createCalendarTable(year, month)} </div> `; return calendarHtml; } ``` 使用方法: ```javascript // 创建 2021 年 10 月的万年历 const calendarHtml = createCalendar(2021, 10); // 将 HTML 插入到页面中 document.body.innerHTML = calendarHtml; ``` 这样就可以在页面上显示出 2021 年 10 月的万年历了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值