请问怎么让表格横向排列?

我通过点击下标‘∨’,生成第二张日历。


生成的第二张表格会使第一张表格上移,从而导致下标‘∨’和’确定‘会遮挡掉一些日期,不美观。
所以我的解决方法是:让点击‘∨’后新生成的表格,往右边移动。

但是没有效果。
同时,我’往下还原‘后,下标‘∨’的位置会改变。这个问题该如何解决?我想要不论是否点击了’向下还原‘,下标‘∨’都在一个位置。


请问,前辈们,这个两个问题该怎么解决怎么实现?
以下是我的代码:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Daily report generator</title>
    <style>
 * {
            margin: 0;
            padding: 0;
        }
        .box {
            float: right;
            margin-right: -840px;
            margin-top: 200px;
            width: 10px;
            position: absolute;
        }

        .title {
            background: linear-gradient(#141e30);

        }

        table {
            height: 200px;
            width: 200px;
            font-size: 12px;
            text-align: center;
            float: right;
            margin: 10px;
            font-family: arial;
        }

        .dash_border {
            font-family: Arial;
            font-size: 1px;
            line-height: 1px;
            font-weight: bold;
            color: pink;
            padding: 0.1px;
            border: 0.1px dashed #FF5722;
            background:
                linear-gradient(to top, #ccc, 10px, transparent 1px),
                linear-gradient(to right, ccc, 10px, transparent 1px),
                linear-gradient(to bottom, ccc, 10px, transparent 1px),
                linear-gradient(to left, ccc, 10px, transparent 1px);
            background-origin: border-box;
        }

        .button_sty { /*确定*/
            float: right;
            margin-right: -780px;
            position: relative;
            top: 120px;
            margin-top: -200px;
            font-size: 13px;
            background: -webkit-linear-gradient(left, #03e9f4, transparent);
            color: #03e9f4;
            
        }

        .add_sty { /*∨*/
            float: right;
            margin-right:515px ;
            font-size: 20px;
            top: 12px;
            position: relative;
            height: 28px;
            width: 30px;
            margin-top: -380px;

        }
        /*----------------------------------输入框----------------------------------------*/
        a {
            text-decoration: none;
        }
        input {
            background: transparent;
            border: 0;
            outline: none;
        }
        .loginBox {
            width: 400px;
            height: 364px;
            background-color: #00000060;
            margin: auto;
            border-radius: 10px;
            box-shadow: 0 15px 25px 0 rgba(0, 0, 0, .6);
            padding: 40px;
            box-sizing: border-box;
            float: auto;
            position: absolute;
        }
       
        h2 {
            text-align: center;
            color: aliceblue;
            margin-bottom: 30px;
            font-family: 'Courier New', Courier, monospace;
        }

        .item {
            height: 45px;
            border-bottom: 1px solid #fff;
            margin-bottom: 40px;
            position: relative;
        }

        .item input {
            width: 100%;
            height: 100%;
            color: #fff;
            padding-top: 20px;
            box-sizing: border-box;
        }

        .item input:focus+label,
        .item input:valid+label {
            top: 0px;
            font-size: 2px;
        }

        .item label {
            position: absolute;
            left: 0;
            top: 12px;
            transition: all 0.5s linear;
        }

        .btn {
            padding: 10px 20px;
            margin-top: 30px;
            color: #03e9f4;
            position: relative;
            overflow: hidden;
            text-transform: uppercase;
            letter-spacing: 2px;
            left: 35%;
        }

        .btn:hover {
            border-radius: 5px;
            color: #fff;
            background: #03e9f4;
            box-shadow: 0 0 5px 0 #03e9f4,
                0 0 25px 0 #03e9f4,
                0 0 50px 0 #03e9f4,
                0 0 100px 0 #03e9f4;
            transition: all 1s linear;
        }

        .btn>span {
            position: absolute;
        }

        .btn>span:nth-child(1) {
            width: 100%;
            height: 2px;
            background: -webkit-linear-gradient(left, transparent, #03e9f4);
            left: -100%;
            top: 0px;
            animation: line1 1s linear infinite;
        }

        @keyframes line1 {

            50%,
            100% {
                left: 100%;
            }
        }

        .btn>span:nth-child(2) {
            width: 2px;
            height: 100%;
            background: -webkit-linear-gradient(top, transparent, #03e9f4);
            right: 0px;
            top: -100%;
            animation: line2 1s 0.25s linear infinite;
        }

        @keyframes line2 {

            50%,
            100% {
                top: 100%;
            }
        }

        .btn>span:nth-child(3) {
            width: 100%;
            height: 2px;
            background: -webkit-linear-gradient(left, #03e9f4, transparent);
            left: 100%;
            bottom: 0px;
            animation: line3 1s 0.75s linear infinite;
        }

        @keyframes line3 {

            50%,
            100% {
                left: -100%;
            }
        }

        .btn>span:nth-child(4) {
            width: 2px;
            height: 100%;
            background: -webkit-linear-gradient(top, transparent, #03e9f4);
            left: 0px;
            top: 100%;
            animation: line4 1s 1s linear infinite;
        }

        @keyframes line4 {
            50%,
            100% {
                top: -100%;
            }
        }
        body {
            height: 100vh;
            background: linear-gradient(#141e30, #243b55);
            display: flex;
            justify-content: center;
            align-items: center;
            font-size: 16px;
            color: #03e9f4;
            background-size: 100% 120%;

        }

    </style>
</head>
<body>
    <div class="loginBox ">
        <h2 >Generator</h2>
        <form action="">
            <div class="item">
                <input type="text" required>
                <label for="" style="font-size: 16px; color: #03e9f4;">Data</label>
            </div>
            <div class="item">
                <input type="password" required>
                <label for=""style="font-size: 16px; color: #03e9f4;">Date</label>
            </div>
            <button class="btn" style="background: transparent; border: 0;outline: none;">generate
                <span></span>
                <span></span>
                <span></span>
                <span></span>
            </button>
        </form>
    </div>
</body>
<script src="https://code.jquery.com/jquery-3.6.3.min.js"
    integrity="sha256-pvPw+upLPUjgMXY0G+8O0xUf+/Im1MZjXxxgOcBQBXU=" crossorigin="anonymous">
    </script>
<script>
    var data_now = new Date();
    var year = data_now.getFullYear();
    var month_now = data_now.getMonth();
    console.log(year)

    var w = new Date(year, 0).getDay();
    var sign2='sec'; //用来区分id
    var html = '<div class="box">';

    //获取‘∨’的点击次数
    $('<button class=add_cla>∨</button>').insertAfter('body')
    $('.add_cla').addClass('add_sty')
    var click_xia = 0; //点击‘∨’的次数
    $('.add_cla').on('click', function () {
        click_xia++;
        if (click_xia == 1) {
            draw(month_now + 2)
        }
        if (click_xia % 2 == 1) {
            $('.table_cla').css('color', 'black');
            $('.title_cla').css('background', '#ccc');

            var td_ele2 = $('.td_event2')// 获取class
            console.log(td_ele2);
            //为td‘点击’添加样式
            td_ele2.click(function () {
                $(this).addClass("dash_border")
                date_value = $(this).text() //储存日期的值
            });
            //为每个td2日期添加点击事件
            for (var i = 0; i < td_ele2.length; i++) {
                // console.log(td_ele[i].innerText);
                td_ele2[i].onclick = getNum(td_ele2[i], function (click_numm, date_numm) {
                    if (click_numm % 2 == 0) {
                        // console.log("点击了偶数次")
                        $(date_numm).removeClass('dash_border');
                        $(date_numm).css("color", "black");
                    } else {
                        // console.log("点击了奇数次")
                        $(date_numm).css("color", "pink");
                    }
                });
            }
        }
        $('.table_cla').fadeToggle(); //淡出
    }
    )
    //拼接每个月份的表格
    function draw(m) {
        calendar = ""
        calendar += "<table class='table_cla' style='display: none; float:right'>";
        calendar += '<tr class="title_cla" ><th colspan="7">' + year + '年' + m + ' 月</th></tr>';
        calendar += '<tr><td>日</td><td>一</td><td>二</td><td>三</td><td>四</td><td>五</td><td>六</td></tr>'
        //获取每个月份共有多少天
        var max = new Date(year, m, 0).getDate();
        calendar += '<tr>';//开始<tr>标签
        for (d = 1; d <= max; d++) {
            if (w && d == 1) {//如果该月的第1天不是星期日,则填充空白
                calendar += '<td colspan ="' + w + '" > </td>'; //变量加入字符串
            }
            calendar += '<td class="td_event2" id=td_id' +sign2+ d + '>' + d + '</td>';
            if (w == 6 && d != max) {//如果星期六不是该月的最后一天,则换行
                calendar += '</tr><tr>';
            } else if (d == max) {//该月的最后一天,闭合</tr>标签
                calendar += '</tr>';
            }
            w = (w + 1 > 6) ? 0 : w + 1;
        }
        calendar += '</table>';
        $('.box').append(calendar);


    }
    for (m = month_now + 1; m <= month_now + 1; m++) {
        // m=month_now+1
        html += "<table >";
        html += '<tr class="title"><th colspan="7" style="color:#03e9f4">' + year + '年' + m + ' 月</th></tr>';
        html += '<tr><td>日</td><td>一</td><td>二</td><td>三</td><td>四</td><td>五</td><td>六</td></tr>'
        //获取每个月份共有多少天
        var max = new Date(year, m, 0).getDate();
        html += '<tr>';//开始<tr>标签
        for (d = 1; d <= max; d++) {
            if (w && d == 1) {//如果该月的第1天不是星期日,则填充空白
                html += '<td colspan ="' + w + '" > </td>'; //变量加入字符串
            }
            html += '<td class="td_event" id=td_id' + d + '>' + d + '</td>';
            if (w == 6 && d != max) {//如果星期六不是该月的最后一天,则换行
                html += '</tr><tr>';
            } else if (d == max) {//该月的最后一天,闭合</tr>标签
                html += '</tr>';
            }
            w = (w + 1 > 6) ? 0 : w + 1;
        }
        html += '</table>';
    }
    html += '</div>';
    document.write(html);
    //定义一个getNum()函数,用于计算点击次数,偶数次点击时,不加载样式
    function getNum(date_num, callback) {
        var click_num = 0;
        window.click_num = callback;
        return (
            function foo() {
                click_num++;
                console.log(click_num)
                console.log(date_num.innerText + "被点击了" + click_num + "次")
                callback(click_num, date_num);
            }
        )
    }
    //---------------------------------------------主函数----------------------------------------------//
    var td_ele = $('.td_event')// 获取class
    console.log(td_ele);
    var date_value = ""; //储存日期
    $('<button class=button_cla style="background: transparent; border: 0;outline: none;">确定</button>').appendTo('body');
    console.log($('.button_cla'));
    $('.button_cla').addClass('button_sty')
    //为td‘点击’添加样式
    td_ele.click(function () {
        $(this).addClass("dash_border")
        date_value = $(this).text() //储存日期的值
    });
    //为每个日期添加点击事件
    for (var i = 0; i < td_ele.length; i++) {
        // console.log(td_ele[i].innerText);
        td_ele[i].onclick = getNum(td_ele[i], function (click_numm, date_numm) {
            if (click_numm % 2 == 0) {
                // console.log("点击了偶数次")
                $(date_numm).removeClass('dash_border');
                $(date_numm).css("color", "black");
            } else {
                // console.log("点击了奇数次")
                $(date_numm).css("color", "pink");
            }
        });
    }

    //为每个按钮添加点击事件
    for (var i = 0; i < $('.button_cla').length; i++) {
        // console.log($('.button_cla').html);
        $('.button_cla')[i].onclick = getNum($('.button_cla')[i], function (click_numm, date_numm) {
            //监测存在几个"dash_border"样式的td
            for (var i = 1; i <= $('.td_event').length; i++) {
                console.log(i);
                console.log($('.td_event')[i - 1]); //别问,问就是能跑就行。
                //解决遍历td的id
                if ($('#td_id' + i).hasClass('dash_border')) {
                    console.log('日期' + i + '存在样式dash_border');
                    $('#td_id' + i).removeClass('dash_border')
                    $('#td_id' + i).css('color', 'black');
                } else {
                    console.log('日期' + i + "不存在样式")
                }
            }
            for (var i = 1; i <= $('.td_event2').length; i++) {
                console.log(i);
                console.log($('.td_event2')[i - 1]); //别问,问就是能跑就行。
                //解决遍历td的id
                if ($('#td_id' +sign2+ i).hasClass('dash_border')) {
                    console.log(sign2+'日期' + i + '存在样式dash_border');
                    $('#td_id'  +sign2+ i).removeClass('dash_border')
                    $('#td_id' +sign2 + i).css('color', 'black');
                } else {
                    console.log(sign2+'日期' + i + "不存在样式")
                }
            }
        });
    }
</script>

</html>
阅读 2.1k
3 个回答
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Daily report generator</title>
    <style>
 * {
            margin: 0;
            padding: 0;
        }
        .box {
            float: right;
            margin-right: -840px;
            margin-top: 200px;
            width: 10px;
            position: absolute;
        }

        .title {
            background: linear-gradient(#141e30);

        }

        table {
            height: 200px;
            width: 200px;
            font-size: 12px;
            text-align: center;
            float: right;
            margin: 10px;
            font-family: arial;
        }

        .dash_border {
            font-family: Arial;
            font-size: 1px;
            line-height: 1px;
            font-weight: bold;
            color: pink;
            padding: 0.1px;
            border: 0.1px dashed #FF5722;
            background:
                linear-gradient(to top, #ccc, 10px, transparent 1px),
                linear-gradient(to right, ccc, 10px, transparent 1px),
                linear-gradient(to bottom, ccc, 10px, transparent 1px),
                linear-gradient(to left, ccc, 10px, transparent 1px);
            background-origin: border-box;
        }

        .button_sty { /*确定*/
            position: absolute;
            right: 0;
            top: 50%;
            transform: translateY(-50%);
            background: -webkit-linear-gradient(left, #03e9f4, transparent);
            color: #03e9f4;
        }

        .add_sty { /*∨*/
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 30px;
            height: 28px;
        }
        /*----------------------------------输入框----------------------------------------*/
        a {
            text-decoration: none;
        }
        input {
            background: transparent;
            border: 0;
            outline: none;
        }
        .loginBox {
            width: 400px;
            height: 364px;
            background-color: #00000060;
            margin: auto;
            border-radius: 10px;
            box-shadow: 0 15px 25px 0 rgba(0, 0, 0, .6);
            padding: 40px;
            box-sizing: border-box;
            float: auto;
            position: absolute;
        }
       
        h2 {
            text-align: center;
            color: aliceblue;
            margin-bottom: 30px;
            font-family: 'Courier New', Courier, monospace;
        }

        .item {
            height: 45px;
            border-bottom: 1px solid #fff;
            margin-bottom: 40px;
            position: relative;
        }

        .item input {
            width: 100%;
            height: 100%;
            color: #fff;
            padding-top: 20px;
            box-sizing: border-box;
        }

        .item input:focus+label,
        .item input:valid+label {
            top: 0px;
            font-size: 2px;
        }

        .item label {
            position: absolute;
            left: 0;
            top: 12px;
            transition: all 0.5s linear;
        }

        .btn {
            padding: 10px 20px;
            margin-top: 30px;
            color: #03e9f4;
            position: relative;
            overflow: hidden;
            text-transform: uppercase;
            letter-spacing: 2px;
            left: 35%;
        }

        .btn:hover {
            border-radius: 5px;
            color: #fff;
            background: #03e9f4;
            box-shadow: 0 0 5px 0 #03e9f4,
                0 0 25px 0 #03e9f4,
                0 0 50px 0 #03e9f4,
                0 0 100px 0 #03e9f4;
            transition: all 1s linear;
        }

        .btn>span {
            position: absolute;
        }

        .btn>span:nth-child(1) {
            width: 100%;
            height: 2px;
            background: -webkit-linear-gradient(left, transparent, #03e9f4);
            left: -100%;
            top: 0px;
            animation: line1 1s linear infinite;
        }

        @keyframes line1 {

            50%,
            100% {
                left: 100%;
            }
        }

        .btn>span:nth-child(2) {
            width: 2px;
            height: 100%;
            background: -webkit-linear-gradient(top, transparent, #03e9f4);
            right: 0px;
            top: -100%;
            animation: line2 1s 0.25s linear infinite;
        }

        @keyframes line2 {

            50%,
            100% {
                top: 100%;
            }
        }

        .btn>span:nth-child(3) {
            width: 100%;
            height: 2px;
            background: -webkit-linear-gradient(left, #03e9f4, transparent);
            left: 100%;
            bottom: 0px;
            animation: line3 1s 0.75s linear infinite;
        }

        @keyframes line3 {

            50%,
            100% {
                left: -100%;
            }
        }

        .btn>span:nth-child(4) {
            width: 2px;
            height: 100%;
            background: -webkit-linear-gradient(top, transparent, #03e9f4);
            left: 0px;
            top: 100%;
            animation: line4 1s 1s linear infinite;
        }

        @keyframes line4 {
            50%,
            100% {
                top: -100%;
            }
        }
        body {
            height: 100vh;
            background: linear-gradient(#141e30, #243b55);
            display: flex;
            justify-content: center;
            align-items: center;
            font-size: 16px;
            color: #03e9f4;
            background-size: 100% 120%;

        }

    </style>
</head>
<body>
    <div class="loginBox ">
        <h2 >Generator</h2>
        <form action="">
            <div class="item">
                <input type="text" required>
                <label for="" style="font-size: 16px; color: #03e9f4;">Data</label>
            </div>
            <div class="item">
                <input type="password" required>
                <label for=""style="font-size: 16px; color: #03e9f4;">Date</label>
            </div>
            <button class="btn" style="background: transparent; border: 0;outline: none;">generate
                <span></span>
                <span></span>
                <span></span>
                <span></span>
            </button>
        </form>
    </div>
</body>
<script src="https://code.jquery.com/jquery-3.6.3.min.js"
    integrity="sha256-pvPw+upLPUjgMXY0G+8O0xUf+/Im1MZjXxxgOcBQBXU=" crossorigin="anonymous">
    </script>
<script>
    var data_now = new Date();
    var year = data_now.getFullYear();
    var month_now = data_now.getMonth();
    console.log(year)

    var w = new Date(year, 0).getDay();
    var sign2='sec'; //用来区分id
    var html = '<div class="box">';

    //获取‘∨’的点击次数
    // $('<button class=add_cla>∨</button>').insertAfter('body')
    // $('.add_cla').addClass('add_sty')
   
    //拼接每个月份的表格
    function draw(m) {
        calendar = ""
        calendar += "<table class='table_cla' style='display: none; float:right'>";
        calendar += '<tr class="title_cla" ><th colspan="7">' + year + '年' + m + ' 月</th></tr>';
        calendar += '<tr><td>日</td><td>一</td><td>二</td><td>三</td><td>四</td><td>五</td><td>六</td></tr>'
        //获取每个月份共有多少天
        var max = new Date(year, m, 0).getDate();
        calendar += '<tr>';//开始<tr>标签
        for (d = 1; d <= max; d++) {
            if (w && d == 1) {//如果该月的第1天不是星期日,则填充空白
                calendar += '<td colspan ="' + w + '" > </td>'; //变量加入字符串
            }
            calendar += '<td class="td_event2" id=td_id' +sign2+ d + '>' + d + '</td>';
            if (w == 6 && d != max) {//如果星期六不是该月的最后一天,则换行
                calendar += '</tr><tr>';
            } else if (d == max) {//该月的最后一天,闭合</tr>标签
                calendar += '</tr>';
            }
            w = (w + 1 > 6) ? 0 : w + 1;
        }
        calendar += '</table>';
        $('.box').append(calendar);


    }
    for (m = month_now + 1; m <= month_now + 1; m++) {
        // m=month_now+1
        html += "<table >";
        html += '<tr class="title"><th colspan="7" style="color:#03e9f4;position:relative;"><button class="add_cla add_sty">∨</button><button class="button_cla button_sty" style="background: transparent; border: 0;outline: none;">确定</button>' + year + '年' + m + ' 月</th></tr>';
        html += '<tr><td>日</td><td>一</td><td>二</td><td>三</td><td>四</td><td>五</td><td>六</td></tr>'
        //获取每个月份共有多少天
        var max = new Date(year, m, 0).getDate();
        html += '<tr>';//开始<tr>标签
        for (d = 1; d <= max; d++) {
            if (w && d == 1) {//如果该月的第1天不是星期日,则填充空白
                html += '<td colspan ="' + w + '" > </td>'; //变量加入字符串
            }
            html += '<td class="td_event" id=td_id' + d + '>' + d + '</td>';
            if (w == 6 && d != max) {//如果星期六不是该月的最后一天,则换行
                html += '</tr><tr>';
            } else if (d == max) {//该月的最后一天,闭合</tr>标签
                html += '</tr>';
            }
            w = (w + 1 > 6) ? 0 : w + 1;
        }
        html += '</table>';
    }
    html += '</div>';
    document.write(html);
    var click_xia = 0; //点击‘∨’的次数
    $('.add_cla').on('click', function () {
        click_xia++;
        if (click_xia == 1) {
            draw(month_now + 2)
        }
        if (click_xia % 2 == 1) {
            $('.table_cla').css('color', 'black');
            $('.title_cla').css('background', '#ccc');

            var td_ele2 = $('.td_event2')// 获取class
            console.log(td_ele2);
            //为td‘点击’添加样式
            td_ele2.click(function () {
                $(this).addClass("dash_border")
                date_value = $(this).text() //储存日期的值
            });
            //为每个td2日期添加点击事件
            for (var i = 0; i < td_ele2.length; i++) {
                // console.log(td_ele[i].innerText);
                td_ele2[i].onclick = getNum(td_ele2[i], function (click_numm, date_numm) {
                    if (click_numm % 2 == 0) {
                        // console.log("点击了偶数次")
                        $(date_numm).removeClass('dash_border');
                        $(date_numm).css("color", "black");
                    } else {
                        // console.log("点击了奇数次")
                        $(date_numm).css("color", "pink");
                    }
                });
            }
        }
        $('.table_cla').fadeToggle(); //淡出
    }
    )
    //定义一个getNum()函数,用于计算点击次数,偶数次点击时,不加载样式
    function getNum(date_num, callback) {
        var click_num = 0;
        window.click_num = callback;
        return (
            function foo() {
                click_num++;
                console.log(click_num)
                console.log(date_num.innerText + "被点击了" + click_num + "次")
                callback(click_num, date_num);
            }
        )
    }
    //---------------------------------------------主函数----------------------------------------------//
    var td_ele = $('.td_event')// 获取class
    console.log(td_ele);
    var date_value = ""; //储存日期
    // $('<button class=button_cla style="background: transparent; border: 0;outline: none;">确定</button>').appendTo('body');
    // console.log($('.button_cla'));
    // $('.button_cla').addClass('button_sty')
    //为td‘点击’添加样式
    td_ele.click(function () {
        $(this).addClass("dash_border")
        date_value = $(this).text() //储存日期的值
    });
    //为每个日期添加点击事件
    for (var i = 0; i < td_ele.length; i++) {
        // console.log(td_ele[i].innerText);
        td_ele[i].onclick = getNum(td_ele[i], function (click_numm, date_numm) {
            if (click_numm % 2 == 0) {
                // console.log("点击了偶数次")
                $(date_numm).removeClass('dash_border');
                $(date_numm).css("color", "black");
            } else {
                // console.log("点击了奇数次")
                $(date_numm).css("color", "pink");
            }
        });
    }

    //为每个按钮添加点击事件
    for (var i = 0; i < $('.button_cla').length; i++) {
        // console.log($('.button_cla').html);
        $('.button_cla')[i].onclick = getNum($('.button_cla')[i], function (click_numm, date_numm) {
            //监测存在几个"dash_border"样式的td
            for (var i = 1; i <= $('.td_event').length; i++) {
                console.log(i);
                console.log($('.td_event')[i - 1]); //别问,问就是能跑就行。
                //解决遍历td的id
                if ($('#td_id' + i).hasClass('dash_border')) {
                    console.log('日期' + i + '存在样式dash_border');
                    $('#td_id' + i).removeClass('dash_border')
                    $('#td_id' + i).css('color', 'black');
                } else {
                    console.log('日期' + i + "不存在样式")
                }
            }
            for (var i = 1; i <= $('.td_event2').length; i++) {
                console.log(i);
                console.log($('.td_event2')[i - 1]); //别问,问就是能跑就行。
                //解决遍历td的id
                if ($('#td_id' +sign2+ i).hasClass('dash_border')) {
                    console.log(sign2+'日期' + i + '存在样式dash_border');
                    $('#td_id'  +sign2+ i).removeClass('dash_border')
                    $('#td_id' +sign2 + i).css('color', 'black');
                } else {
                    console.log(sign2+'日期' + i + "不存在样式")
                }
            }
        });
    }
</script>

</html>

你的代码插入一个table,而table默认是block
给table添加样式处理一下成为行内元素就行.
更正: 设置为行内块会导致table样式丑化, 外层再包裹一层用于处理行内块或者flex布局.

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>Daily report generator</title>
        <style>
            * {
                margin: 0;
                padding: 0;
            }
            .box {
                float: right;
                margin-right: -840px;
                margin-top: 200px;
                width: 10px;
                position: absolute;
            }

            .title {
                background: linear-gradient(#141e30);
            }

            table {
                height: 200px;
                width: 200px;
                font-size: 12px;
                text-align: center;
                float: right;
                margin: 10px;
                font-family: arial;
            }

            .dash_border {
                font-family: Arial;
                font-size: 1px;
                line-height: 1px;
                font-weight: bold;
                color: pink;
                padding: 0.1px;
                border: 0.1px dashed #ff5722;
                background: linear-gradient(to top, #ccc, 10px, transparent 1px), linear-gradient(to right, ccc, 10px, transparent 1px),
                    linear-gradient(to bottom, ccc, 10px, transparent 1px), linear-gradient(to left, ccc, 10px, transparent 1px);
                background-origin: border-box;
            }

            .button_sty {
                /*确定*/
                float: right;
                margin-right: -780px;
                position: relative;
                top: 120px;
                margin-top: -200px;
                font-size: 13px;
                background: -webkit-linear-gradient(left, #03e9f4, transparent);
                color: #03e9f4;
            }

            .add_sty {
                /*∨*/
                float: right;
                margin-right: 515px;
                font-size: 20px;
                top: 12px;
                position: relative;
                height: 28px;
                width: 30px;
                margin-top: -380px;
            }
            /*----------------------------------输入框----------------------------------------*/
            a {
                text-decoration: none;
            }
            input {
                background: transparent;
                border: 0;
                outline: none;
            }
            .loginBox {
                width: 400px;
                height: 364px;
                background-color: #00000060;
                margin: auto;
                border-radius: 10px;
                box-shadow: 0 15px 25px 0 rgba(0, 0, 0, 0.6);
                padding: 40px;
                box-sizing: border-box;
                float: auto;
                position: absolute;
            }

            h2 {
                text-align: center;
                color: aliceblue;
                margin-bottom: 30px;
                font-family: "Courier New", Courier, monospace;
            }

            .item {
                height: 45px;
                border-bottom: 1px solid #fff;
                margin-bottom: 40px;
                position: relative;
            }

            .item input {
                width: 100%;
                height: 100%;
                color: #fff;
                padding-top: 20px;
                box-sizing: border-box;
            }

            .item input:focus + label,
            .item input:valid + label {
                top: 0px;
                font-size: 2px;
            }

            .item label {
                position: absolute;
                left: 0;
                top: 12px;
                transition: all 0.5s linear;
            }

            .btn {
                padding: 10px 20px;
                margin-top: 30px;
                color: #03e9f4;
                position: relative;
                overflow: hidden;
                text-transform: uppercase;
                letter-spacing: 2px;
                left: 35%;
            }

            .btn:hover {
                border-radius: 5px;
                color: #fff;
                background: #03e9f4;
                box-shadow: 0 0 5px 0 #03e9f4, 0 0 25px 0 #03e9f4, 0 0 50px 0 #03e9f4, 0 0 100px 0 #03e9f4;
                transition: all 1s linear;
            }

            .btn > span {
                position: absolute;
            }

            .btn > span:nth-child(1) {
                width: 100%;
                height: 2px;
                background: -webkit-linear-gradient(left, transparent, #03e9f4);
                left: -100%;
                top: 0px;
                animation: line1 1s linear infinite;
            }

            @keyframes line1 {
                50%,
                100% {
                    left: 100%;
                }
            }

            .btn > span:nth-child(2) {
                width: 2px;
                height: 100%;
                background: -webkit-linear-gradient(top, transparent, #03e9f4);
                right: 0px;
                top: -100%;
                animation: line2 1s 0.25s linear infinite;
            }

            @keyframes line2 {
                50%,
                100% {
                    top: 100%;
                }
            }

            .btn > span:nth-child(3) {
                width: 100%;
                height: 2px;
                background: -webkit-linear-gradient(left, #03e9f4, transparent);
                left: 100%;
                bottom: 0px;
                animation: line3 1s 0.75s linear infinite;
            }

            @keyframes line3 {
                50%,
                100% {
                    left: -100%;
                }
            }

            .btn > span:nth-child(4) {
                width: 2px;
                height: 100%;
                background: -webkit-linear-gradient(top, transparent, #03e9f4);
                left: 0px;
                top: 100%;
                animation: line4 1s 1s linear infinite;
            }

            @keyframes line4 {
                50%,
                100% {
                    top: -100%;
                }
            }
            body {
                height: 100vh;
                background: linear-gradient(#141e30, #243b55);
                display: flex;
                justify-content: center;
                align-items: center;
                font-size: 16px;
                color: #03e9f4;
                background-size: 100% 120%;
            }
            .table_cla {
                position: absolute;
                top: 200px;
                left: -212px;
            }
        </style>
    </head>
    <body>
        <div class="loginBox">
            <h2>Generator</h2>
            <form action="">
                <div class="item">
                    <input type="text" required />
                    <label for="" style="font-size: 16px; color: #03e9f4">Data</label>
                </div>
                <div class="item">
                    <input type="password" required />
                    <label for="" style="font-size: 16px; color: #03e9f4">Date</label>
                </div>
                <button class="btn" style="background: transparent; border: 0; outline: none">
                    generate
                    <span></span>
                    <span></span>
                    <span></span>
                    <span></span>
                </button>
            </form>
        </div>
    </body>
    <script src="https://code.jquery.com/jquery-3.6.3.min.js" integrity="sha256-pvPw+upLPUjgMXY0G+8O0xUf+/Im1MZjXxxgOcBQBXU=" crossorigin="anonymous"></script>
    <script>
        var data_now = new Date();
        var year = data_now.getFullYear();
        var month_now = data_now.getMonth();
        console.log(year);

        var w = new Date(year, 0).getDay();
        var sign2 = "sec"; //用来区分id
        var html = '<div class="box">';

        //获取‘∨’的点击次数
        $("<button class=add_cla>∨</button>").insertAfter("body");
        $(".add_cla").addClass("add_sty");
        var click_xia = 0; //点击‘∨’的次数
        $(".add_cla").on("click", function () {
            click_xia++;
            if (click_xia == 1) {
                draw(month_now + 2);
            }
            if (click_xia % 2 == 1) {
                $(".table_cla").css("color", "black");
                $(".title_cla").css("background", "#ccc");

                var td_ele2 = $(".td_event2"); // 获取class
                console.log(td_ele2);
                //为td‘点击’添加样式
                td_ele2.click(function () {
                    $(this).addClass("dash_border");
                    date_value = $(this).text(); //储存日期的值
                });
                //为每个td2日期添加点击事件
                for (var i = 0; i < td_ele2.length; i++) {
                    // console.log(td_ele[i].innerText);
                    td_ele2[i].onclick = getNum(td_ele2[i], function (click_numm, date_numm) {
                        if (click_numm % 2 == 0) {
                            // console.log("点击了偶数次")
                            $(date_numm).removeClass("dash_border");
                            $(date_numm).css("color", "black");
                        } else {
                            // console.log("点击了奇数次")
                            $(date_numm).css("color", "pink");
                        }
                    });
                }
            }
            $(".table_cla").fadeToggle(); //淡出
        });
        //拼接每个月份的表格
        function draw(m) {
            calendar = "";
            calendar += "<table class='table_cla' style='display: none; float:right'>";
            calendar += '<tr class="title_cla" ><th colspan="7">' + year + "年" + m + " 月</th></tr>";
            calendar += "<tr><td>日</td><td>一</td><td>二</td><td>三</td><td>四</td><td>五</td><td>六</td></tr>";
            //获取每个月份共有多少天
            var max = new Date(year, m, 0).getDate();
            calendar += "<tr>"; //开始<tr>标签
            for (d = 1; d <= max; d++) {
                if (w && d == 1) {
                    //如果该月的第1天不是星期日,则填充空白
                    calendar += '<td colspan ="' + w + '" > </td>'; //变量加入字符串
                }
                calendar += '<td class="td_event2" id=td_id' + sign2 + d + ">" + d + "</td>";
                if (w == 6 && d != max) {
                    //如果星期六不是该月的最后一天,则换行
                    calendar += "</tr><tr>";
                } else if (d == max) {
                    //该月的最后一天,闭合</tr>标签
                    calendar += "</tr>";
                }
                w = w + 1 > 6 ? 0 : w + 1;
            }
            calendar += "</table>";
            $(".box").append(calendar);
        }
        for (m = month_now + 1; m <= month_now + 1; m++) {
            // m=month_now+1
            html += "<table >";
            html += '<tr class="title"><th colspan="7" style="color:#03e9f4">' + year + "年" + m + " 月</th></tr>";
            html += "<tr><td>日</td><td>一</td><td>二</td><td>三</td><td>四</td><td>五</td><td>六</td></tr>";
            //获取每个月份共有多少天
            var max = new Date(year, m, 0).getDate();
            html += "<tr>"; //开始<tr>标签
            for (d = 1; d <= max; d++) {
                if (w && d == 1) {
                    //如果该月的第1天不是星期日,则填充空白
                    html += '<td colspan ="' + w + '" > </td>'; //变量加入字符串
                }
                html += '<td class="td_event" id=td_id' + d + ">" + d + "</td>";
                if (w == 6 && d != max) {
                    //如果星期六不是该月的最后一天,则换行
                    html += "</tr><tr>";
                } else if (d == max) {
                    //该月的最后一天,闭合</tr>标签
                    html += "</tr>";
                }
                w = w + 1 > 6 ? 0 : w + 1;
            }
            html += "</table>";
        }
        html += "</div>";
        document.write(html);
        //定义一个getNum()函数,用于计算点击次数,偶数次点击时,不加载样式
        function getNum(date_num, callback) {
            var click_num = 0;
            window.click_num = callback;
            return function foo() {
                click_num++;
                console.log(click_num);
                console.log(date_num.innerText + "被点击了" + click_num + "次");
                callback(click_num, date_num);
            };
        }
        //---------------------------------------------主函数----------------------------------------------//
        var td_ele = $(".td_event"); // 获取class
        console.log(td_ele);
        var date_value = ""; //储存日期
        $('<button class=button_cla style="background: transparent; border: 0;outline: none;">确定</button>').appendTo("body");
        console.log($(".button_cla"));
        $(".button_cla").addClass("button_sty");
        //为td‘点击’添加样式
        td_ele.click(function () {
            $(this).addClass("dash_border");
            date_value = $(this).text(); //储存日期的值
        });
        //为每个日期添加点击事件
        for (var i = 0; i < td_ele.length; i++) {
            // console.log(td_ele[i].innerText);
            td_ele[i].onclick = getNum(td_ele[i], function (click_numm, date_numm) {
                if (click_numm % 2 == 0) {
                    // console.log("点击了偶数次")
                    $(date_numm).removeClass("dash_border");
                    $(date_numm).css("color", "black");
                } else {
                    // console.log("点击了奇数次")
                    $(date_numm).css("color", "pink");
                }
            });
        }

        //为每个按钮添加点击事件
        for (var i = 0; i < $(".button_cla").length; i++) {
            // console.log($('.button_cla').html);
            $(".button_cla")[i].onclick = getNum($(".button_cla")[i], function (click_numm, date_numm) {
                //监测存在几个"dash_border"样式的td
                for (var i = 1; i <= $(".td_event").length; i++) {
                    console.log(i);
                    console.log($(".td_event")[i - 1]); //别问,问就是能跑就行。
                    //解决遍历td的id
                    if ($("#td_id" + i).hasClass("dash_border")) {
                        console.log("日期" + i + "存在样式dash_border");
                        $("#td_id" + i).removeClass("dash_border");
                        $("#td_id" + i).css("color", "black");
                    } else {
                        console.log("日期" + i + "不存在样式");
                    }
                }
                for (var i = 1; i <= $(".td_event2").length; i++) {
                    console.log(i);
                    console.log($(".td_event2")[i - 1]); //别问,问就是能跑就行。
                    //解决遍历td的id
                    if ($("#td_id" + sign2 + i).hasClass("dash_border")) {
                        console.log(sign2 + "日期" + i + "存在样式dash_border");
                        $("#td_id" + sign2 + i).removeClass("dash_border");
                        $("#td_id" + sign2 + i).css("color", "black");
                    } else {
                        console.log(sign2 + "日期" + i + "不存在样式");
                    }
                }
            });
        }
    </script>
</html>
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题