[英]Uncaught (in promise) TypeError: Cannot set property 'innerHTML' of null
I understand there are questions similar to this issue. 我了解有些问题与此问题类似。 And I have taken the time to look through them.
我花了一些时间仔细研究它们。 But the implementation here is a little different than in some of the cases I looked at.
但是,这里的实现与我研究的某些情况有些不同。 The good news here is my fourth column, the running totals column, is correctly displaying the data I want.
好消息是我的第四列,即运行总计列,正在正确显示我想要的数据。 Everything is working (correctly calculated and displaying) so why is my app held up on this?
一切正常(正确计算并显示),那么为什么我的应用程序无法正常运行? I'd appreciate it.
我会很感激的。 Thanks.
谢谢。
calculateRunningTotals(){
var attendantTotalCell = 0;
var total = 0;
var totalCell="";
var totalsCells = document.querySelectorAll("td:nth-child(3)");
totalsCells.forEach(function(cell, index){
console.log("The contents of the totals cell " + cell.innerText);
totalCell = cell.innerText;
attendantTotalCell = totalCell.replace(/[^0-9-.]/g, '');
total = parseInt(attendantTotalCell) + parseInt(total);
console.log("Attendant Total Cell is: " + attendantTotalCell);
console.log("Attendant Total is " + total);
cell.nextElementSibling.innerHTML = total;
})
}
If you add the following check to ensure that cell.nextElementSibling
is defined, then this should resolve your problem: 如果添加以下检查以确保定义了
cell.nextElementSibling
,那么这应该可以解决您的问题:
calculateRunningTotals(){
var attendantTotalCell = 0;
var total = 0;
var totalCell="";
var totalsCells = document.querySelectorAll("td:nth-child(3)");
totalsCells.forEach(function(cell, index){
console.log("The contents of the totals cell " + cell.innerText);
totalCell = cell.innerText;
attendantTotalCell = totalCell.replace(/[^0-9-.]/g, '');
total = parseInt(attendantTotalCell) + parseInt(total);
console.log("Attendant Total Cell is: " + attendantTotalCell);
console.log("Attendant Total is " + total);
/* Add the following check to ensure nextElementSibling is defined
before attempting to access it */
if(cell.nextElementSibling) {
cell.nextElementSibling.innerHTML = total;
}
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.