Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/405.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 基本HTML日历出错_Javascript_Html_Css - Fatal编程技术网

Javascript 基本HTML日历出错

Javascript 基本HTML日历出错,javascript,html,css,Javascript,Html,Css,这是一个基本的日历项目,我想有按钮,通过几个月 该按钮适用于console.log,因此我的匿名函数肯定有问题 const today = new Date(); const calMonth = today.getMonth(); document.querySelector("button").addEventListener("click", event => {calMonth--}); 代码笔上的全部内容: 单击“上一步/下一步”按钮的代码只设置了calMonth的值。另外,注

这是一个基本的日历项目,我想有按钮,通过几个月

该按钮适用于
console.log
,因此我的匿名函数肯定有问题

const today = new Date();
const calMonth = today.getMonth();
document.querySelector("button").addEventListener("click", event => {calMonth--});
代码笔上的全部内容:

单击“上一步/下一步”按钮的代码只设置了
calMonth
的值。另外,注意,
calMonth
被声明为
const
,这意味着您不能更改其值

以下是您可以做的:

  • const
    更改为
    let
  • 将设置getHead的innerHTML并将日期呈现到函数中的代码包装起来

    function renderCalendar() {
      getHead.innerHTML = monthFran[calMonth];
      for (let i in allDays) {
        let printedDay = i - calDate.getDay();
        allDays[i].innerHTML = (printedDay < 1 || printedDay > monthEnd[calMonth]) ? "" : printedDay;
      }
    }
    

  • 当然,该解决方案在任何方面都不是完美的/优化的,只是让您知道您可以做什么。

    不清楚您在此处发布的代码预期会发生什么,因此您可能会收到一些负面反馈。不幸的是,代码笔并不能真正取代在问题正文中发布的。您当前在问题正文中发布的内容表明,您希望更改某些内容,可能是在HTML显示中,但实际上并没有显示任何可以写入HTML的代码,更不用说更新HTML了。
    document.querySelectorAll('button').forEach((btn) => {
      btn.addEventListener('click', (e) => {
        if (e.target.classList.contains('bak')) {
          calMonth--;
        } else {
          calMonth++;
        }
    
        renderCalendar();
      });
    });
    
    renderCalendar();