我正在编写一个函数,可以在单击时切换两个div.我有两个函数将api json数据显示为表和图(formatGraphformatTable).

我有一个if语句来判断何时显示或隐藏div.在呈现结果时,我正在fetch调用中的某个地方调用formatData函数.我有一个包含以下代码的css文件:

.displayTable {
    display: block;
}

.displayGraph{
    display: none;
}

但我现在得到undefined

Uncaught TypeError: Cannot read properties of null (reading 'style')
at HTMLButtonElement.

你知道我错过了什么,或者我该怎么做吗?以下是我的功能:

const formatData = (response) => {
    const mainDiv = document.createElement("div");
    const div1= document.createElement("div");
    div1.classList.add("displayTable");
    div1.appendChild(formatTable(response));
    mainDiv.appendChild(div1)

    const div2 = document.createElement("div");
    div2.classList.add("displayGraph");
    div2.appendChild(formatGraph(response));
    mainDiv.appendChild(div2)
    

    viewTable.addEventListener("click", function() {

    if(document.querySelector('.displayTable').style.display === "none") {
        document.querySelector('.displayGraph').style.display = 'none';
        document.querySelector('.displayTable').style.display = 'block';
    }

    else{
        document.querySelector('.displayTable').style.display === "none"
       document.querySelector('.displayGraph').style.display = 'block';
    }
    
    
})
};

推荐答案

您的代码看起来不错,但您忘记了对文档执行append mainDiv.这就是为什么打document.querySelector(".displayTable")document.querySelector(".displayGraph")得到null分的原因.在null中使用access运算符会导致TypeError.

//...
const div2 = document.createElement("div");
div2.classList.add("displayGraph");
div2.appendChild(formatGraph(response));
mainDiv.appendChild(div2)
document.body.appendChild(mainDiv);
//...

我刚刚在body中使用了.appendChild,它将元素附加在正文的末尾.有不同的方法可以让您在DOM 3的不同位置附加元素.您还可以在HTML中创建一个空元素(没有子元素),并使用该元素作为包装来附加div.

此外,考虑处理程序的改进:

viewTable.addEventListener("click", function() {
  // Execute document.querySelector once.
  const displayTableDiv = document.querySelector('.displayTable')
  const displayGraphDiv = document.querySelector('.displayGraph')
  
  /* This new condition will prevent the Type Error if any. */
  if (displayTableDiv && displayGraphDiv) {
    if(displayTableDiv.style.display === "none") {
      displayGraphDiv.style.display = 'none';
      displayTableDiv.style.display = 'block';
    }
    else {
      displayTableDiv.style.display === "none"
      displayGraphDiv.style.display = 'block';
    }
  }
})

Javascript相关问答推荐

积分计算和 colored颜色 判断错误

vanillajs-datepicker未设置输入值,日期单击时未触发更改事件

如何使用Echart 5.5.0创建箱形图

通过在页面上滚动来移动滚动条

django无法解析余数:[0] from carray[0]'

Google图表时间轴—更改hAxis文本 colored颜色

Plotly热图:在矩形上zoom 后将zoom 区域居中

在react js中使用react—router—dom中的Link组件,分配的右侧不能被 destruct ''

如何在bslib nav_insert之后更改导航标签的CSS类和样式?

JS—删除对象数组中对象的子对象

变量的值在Reaction组件中的Try-Catch语句之外丢失

如何在ASP.NET JavaScript中使用Google Charts API仅对绘制为负方向的条形图移动堆叠条形图标签位置

在运行时使用Next JS App Router在服务器组件中运行自定义函数

确保函数签名中的类型安全:匹配值

在开发期间,Web浏览器如何运行&qot;.jsx&qot;文件?

Reaction useState和useLoaderData的组合使用引发无限循环错误

如何在Reaction中清除输入字段

为什么当雪碧的S在另一个函数中时,Phaser不能加载它?

Firefox的绝对定位没有达到预期效果

KeyboardEvent:检测到键具有打印的表示形式