[英]IE9 Compatibility Mode Error: Unable to get value of the property '0': object is null or undefined
I am trying to get the children of a <tr>
element in a table. 我正在尝试在表中获取
<tr>
元素的子级。 I need to get each of the individual <td>
cells. 我需要获取每个单独的
<td>
单元格。 I cannot use jQuery, only JavaScript for infrastructure reasons. 我不能使用jQuery,仅出于基础结构原因不能使用JavaScript。 My JavaScript looks like this:
我的JavaScript看起来像这样:
var feeTable = document.getElementById("feeTable"); function gatherRowData() { console.log("gathering row data"); var rows = this.parentNode.children; //This is where it fails. var state = rows[0].innerText; var county = rows[1].innerText; var city = rows[2].innerText; console.log("state: " + state); document.getElementById("state_code").value = state; document.getElementById("county_name").value = county; document.getElementById("city_name").value = city; } //Loop through Vehicle Fee Changes to find any empty cells in county, city, or start date. for (var i = 0; row = feeTable.rows[i]; i++) { var county = row.cells[1]; var city = row.cells[2]; var startDate = row.cells[6]; if (county.innerText == "") { county.style.backgroundColor = "#FF0000"; showError(invalidCells); } if (city.innerText == "") { city.style.backgroundColor = "#FF0000"; showError(invalidCells); } if (startDate.innerText == "") { startDate.style.backgroundColor = "#FF0000"; showError(invalidCells); } if (document.addEventListener) { row.cells[8].addEventListener("click", gatherRowData); } else if (document.attachEvent) { //Support for IE row.cells[8].attachEvent("onclick", gatherRowData); } }
The onClick listeners work just fine, but when I try to get the children of the table row, it gives me the error: Error message: Unable to get value of the property '0': object is null or undefined
It works fine in all other browsers, and IE9 without compatibility mode on (it is required to function with compatibility mode). onClick侦听器工作正常,但是当我尝试获取表行的子级时,它给了我错误:
Error message: Unable to get value of the property '0': object is null or undefined
在所有情况下都可以正常工作其他浏览器,以及未启用兼容模式的IE9(需要在兼容模式下运行)。 What am I missing that would allow me to get the children of the tr
element? 我缺少什么让我得到
tr
元素的孩子?
This error may occur when the feeTable has null value 当feeTable的值为空时,可能会发生此错误
var feeTable = document.getElementById("feeTable");
Try writing values of feeTable in console, and check it has proper value other than null. 尝试在控制台中写入feeTable的值,并检查其是否具有除null之外的正确值。
I found a solution to my problem. 我找到了解决问题的方法。 It turns out that when you use attachEvent to attach an event listener, the reference to
this
in the JavaScript event handler function doesn't actually reference the object that is being clicked on. 事实证明,当您使用的attachEvent附加事件侦听器,引用
this
在JavaScript事件处理函数实际上并没有引用被点击的对象。 It represents the Window object as a whole. 它代表了整个Window对象。 I had to use a slightly less elegant solution:
我不得不使用稍微不太优雅的解决方案:
row.cells[8].onclick = gatherRowData;
This is the only solution that would work for the versions of IE that don't support addEventListener
. 这是适用于不支持
addEventListener
的IE版本的唯一解决方案。 Using this solution, the rest of my code worked fine and this
referenced the <td>
element as it was supposed to. 使用该解决方案,我的代码的其余部分工作的罚款和
this
所引用的<td>
元素,因为它是应该。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.