获取<h1>标签的值

时间:2018-11-30 22:33:17

标签: javascript html

我想获得一个不断变化的标题的值,因此我可以根据显示的月份更改日历,但是它始终不返回任何内容或未定义。我将如何使用纯JavaScript或是否有其他方法做到这一点。这是我的代码:

var currentMonthName = n;
const monthNames = ["January", "February", "March", "April", "May", "June",
    "July", "August", "September", "October", "November", "December"];
var calendar = document.getElementById("calendar");
var rowLength = calendar.rows.length;
const d = new Date();
var febOptional2 = document.getElementById("febOptional2");
var monthOptional = document.getElementById("monthOptional");
var febOptional = document.getElementById("febOptional")
var optional = document.getElementsByClassName("optional");
var numDaysInMonth = daysInMonth(d.getMonth(), d.getFullYear());
var n = monthNames[d.getMonth()];
window.onload = function () {
    if (numDaysInMonth === 28) {
        for (var i = 0; i < optional.length; i++) {
            optional[i].style.display = "none";
            var lastTr = document.getElementById("lastTr")
            var filler = document.createElement("td");
            lastTr.appendChild(filler);
        }
    }

    else if (numDaysInMonth === 29) {
        monthOptional.style.display = "none";
        febOptional2.style.display = "none";
        var lastTr = document.getElementById("lastTr")
        var filler = document.createElement("td");
        lastTr.appendChild(filler);
        var lastTr = document.getElementById("lastTr")
        var filler = document.createElement("td");
        lastTr.appendChild(filler);
    }
    else if (numDaysInMonth === 30) {
        monthOptional.style.display = "none";
        var lastTr = document.getElementById("lastTr")
        var filler = document.createElement("td");
        lastTr.appendChild(filler);
    }

    var day = new Date();
    var nextDay = new Date(day);


    for (var o = 0; o < rowLength; o++) {

        var cells = calendar.rows.item(o).cells;

        var cellLength = cells.length;

        for (var j = 0; j < cellLength; j++) {
            var cellVal = cells.item(j).innerHTML;
            if (cellVal == d.getDate()) {
                cells[j].style.outlineStyle = "solid";
                cells[j].style.outlineColor = "blue";
            }
            var tds = document.querySelectorAll('tr.calRow td');

            function checkCal() {
                for (var l = 0; l < tds.length; l++)
                    if (tds[l].textContent === "25" && document.getElementById("monthName").innerHTML.indexOf("December") != -1) {
                        var christmas = document.getElementById("christmas");
                        christmas.style.backgroundColor = "red";
                        var txt = "\r\nChristmas Day";
                        tds[l].textContent += txt;
                        tds[l].classList.add("christmas-day");
                        tds[l].setAttribute("style", "white-space: pre; background: red; font-size: 20px; padding: 0; margin: 0; width: 15%; height: 15vh;");
                    }


                }
            }
        }
    }
    document.getElementById("monthName").innerHTML = n + "\n" + d.getFullYear();
}

function daysInMonth(month, year) {
    return 32 - new Date(year, month, 32).getDate();
}

JSfiddle: http://jsfiddle.net/54mfge2b/

感谢您的帮助!

0 个答案:

没有答案