Uncaught TypeError: Cannot read property 'parentNode' of undefined

二医少年 二医少年 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
本人新人,最近在学javascript,使用chrome调试js时出现Uncaught TypeError: Cannot read property 'parentNode' of undefined,不知道如何解决,希望有人能帮忙,十分感谢·


<!DOCTYPE html>
<html>
<head>
<title> new document </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script type="text/javascript">
window.onload = function(){
var trs = document.getElementsByTagName('tr');
for(var i = 1; i < trs.length; i++){
trs[i].onmouseover = function(){
this.style.backgroundColor = "#f2f2f2";
}
trs[i].onmouseout = function(){
this.style.backgroundColor = "#fff";
}
}
}

function addTo(){
var table = document.getElementById('table');

var tr = document.createElement('tr');

var td = document.createElement('td');
td.innerHTML="<input type = 'text' />";
tr.appendChild(td);

td = document.createElement('td');
td.innerHTML="<input type = 'text' />";
tr.appendChild(td);

td = document.createElement('td');
td.innerHTML="<a href='javascript:deleteItem(this);'>删除</a>";
tr.appendChild(td);

table.appendChild(tr);
}

function deleteItem(obj){
var table = document.getElementById('table');
var tr = obj.parentNode.parentNode; //报错在这一行
table.removeChild(tr);

}
</script>
</head>
<body>
<table border="1" width="50%" id="table">
<tr>
<th>工号</th>
<th>姓名</th>
<th>选项</th>
</tr>

<tr>
<td>01</td>
<td>小明</td>
<td><a href="javascript:deleteItem(this);" >删除</a></td>
</tr>

<tr>
<td>02</td>
<td>小王</td>
<td><a href="javascript:deleteItem(this);" >删除</a></td>
</tr>

</table>
<input type="button" value="添加一行" onclick = "addTo();"/>
</body>
</html>
...全文

相关推荐

dataTool.min.js

解决java中echarts关系图中如何引用dataTool:Uncaught TypeError: Cannot read property 'gexf' of undefined

wanglin19900225

我敢说在座的所有人都遇到过这些JS常见的错误【谨以此篇记录成长】

一、TypeError(类型错误) (1) TypeError:Uncaught TypeError: Cannot read property 'XXX' of undefined 调用了对象不存在的方法。 (2) TypeError:'undefined'is not an object 多半是使用了某个变量,拿到了一个尚未分配的变量,即undefind,然后用又undefined去做了一些事情。比如说:绑定到某些dom上面等… (3) TypeError: 'null' is not an object null 表示该值为空。这种错误可能发生场景是:在加载元素之前尝试在 JS中

weixin_38700320

解决浏览器报错:Uncaught TypeError[项目源码]

文章详细分析了浏览器控制台报错“Uncaught TypeError: Cannot read properties of undefined (reading ‘forEach’)”的原因及解决方法。该错误通常是由于调用forEach方法的对象为undefined或未定义。解决思路包括查看控制台报错指向的位置,检查并确保调用forEach的对象不为空。具体步骤包括在相关函数中打印调用forEach的对象,确认其是否为undefined,并通过解决fileList对象为空的问题来修复错误。

hp777

10种JavaScript最常见的错误(小结)

主要介绍了10种JavaScript最常见的错误(小结),查看了数千个项目后,发现了 10 个最常见的 JavaScript 错误。我们会告诉你什么原因导致了这些错误,以及如何防止这些错误发生

weixin_38706747

10 种最常见的 Javascript 错误(频率最高)

本文是小编给大家收藏的JavaScript 中频度最高的 10 种错误,我们会告诉你什么原因导致了这些错误,以及如何防止这些错误发生。需要的朋友参考下

weixin_38625098
评论 4
收藏 已收藏
分享
返回顶部
二医少年
JavaScript 87,983 成员 224,681 内容