简体   繁体   English

IE9兼容模式错误:无法获取属性“ 0”的值:对象为null或未定义

[英]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.

相关问题 IE9 JavaScript 错误:SCRIPT5007:无法获取属性“ui”的值:对象为空或未定义 - IE9 JavaScript error: SCRIPT5007: Unable to get value of the property 'ui': object is null or undefined IE9中Magento的Lightbox JS错误-无法获取属性“ 0”的值:对象为null或未定义 - Lightbox JS Error on Magento in IE9 - Unable to get value of the property '0': object is null or undefined IE9中的Angular 1/2混合应用程序错误:“ TypeError:无法获取属性&#39;ref&#39;的值:对象为null或未定义” - Angular 1/2 hybrid app error in ie9: “TypeError: Unable to get value of the property 'ref': object is null or undefined” Microsoft JScript运行时错误:无法获取属性&#39;checked&#39;的值:对象为null或仅在IE9中未定义 - Microsoft JScript runtime error: Unable to get value of the property 'checked': object is null or undefined only in IE9 在IE9中加载页面时如何防止“无法获取属性&#39;dir&#39;的值:对象为空或未定义”错误 - How to prevent “Unable to get value of the property 'dir': object is null or undefined” error when loading pages in IE9 使用IE9设置表单输入值(出现错误-无法设置属性“值”的值:对象为null或未定义) - Setting a Form Input Value With IE9 (Getting An Error - Unable to set value of the property 'value': object is null or undefined) IE9 JS无法获取属性&#39;display&#39;的值:object为null或undefined - IE9 JS Unable to get value of the property 'display': object is null or undefined jQuery IE9:无法获取属性&#39;val&#39;的值:对象为null或未定义 - jQuery IE9: Unable to get value of the property 'val': object is null or undefined IE9无法获取未定义或空引用的属性“删除” - IE9 unable to get property 'remove' of undefined or null reference (IE9)无法获取未定义或空引用的属性&#39;dispatchEvent&#39; - (IE9)Unable to get property 'dispatchEvent' of undefined or null reference
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM