0

我想删除表格中的一个元素,

我尝试使用 tr.removeChild(tr.childNodes[2]);

删除我的第三行元素,但它并没有真正起作用

function smile()
{
document.getElementById("p1").style.backgroundImage = 'url(smile.jpg)';
}
function twice()
{
document.getElementById("p2").innerHTML="Smile Smile";
}
function empty()
    {
//this is the part i want to remove , i want to remove the text inside it
    tr.removeChild(tr.childNodes[3]);
}

</script>
</head>

<body>

<table border = "1" >
<form>
<tr>
<th onclick="smile()"><input type="button" name="person1" value="Make it smile!" id="person4" size="30" </th>
<th onclick="twice()"><input type="button" name="person1" value="Make it smile twice!" id="person4" size="30" </th>
<th onclick="empty()"><input type="button" name="person1" value="Make it empty" id="person4" size="30" </th>
</tr>
4

2 回答 2

0

几件事:

  • 您需要tr在函数内部进行设置empty(),现在它没有设置为任何内容。

  • 数组的索引从 0 开始,因此您要告诉它删除第 4 个孩子。尝试将其更改为:

    tr.removeChild(tr.childNodes[2]);

于 2012-12-14T05:43:52.090 回答
0
于 2012-12-14T05:55:54.573 回答