我正在编写一个函数,可以在单击时切换两个div.我有两个函数将api json数据显示为表和图(formatGraph
和formatTable
).
我有一个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';
}
})
};