"的 parentNode 是什么?-6ren"> "的 parentNode 是什么?-我试过这个: aa bb 和javascript: function test(){ var parent=document.getElementById("table1"); -6ren">
gpt4 book ai didi

javascript - ""的 parentNode 是什么?

转载 作者:行者123 更新时间:2023-11-30 10:28:12 24 4
gpt4 key购买 nike

我试过这个:

<table id ="table1">
<tr id ="aa"><td>aa</td></tr>
<tr id ="bb"><td>bb</td></tr>
</table>
<input type="button" onclick = "test()" value = "button">

和javascript:

function test(){
var parent=document.getElementById("table1");
var child=document.getElementById("aa");
parent.removeChild(child); //this isn't work
//child.parentNode.removeChild(child); this is ok!
}

错误:

Uncaught NotFoundError: An attempt was made to reference a Node in a context where it does not exist

于是,我就疑惑了,table不是tr的parent吗?

最佳答案

在这种情况下,父元素是一个 <tbody> (开始和结束标签是可选的)。

参见 HTML 5 tree construction rules :

8.2.5.4.9 The "in table" insertion mode

A start tag whose tag name is one of: "td", "th", "tr"

Clear the stack back to a table context. (See below.)

Insert an HTML element for a "tbody" start tag token with no attributes, then switch the insertion mode to "in table body".

Reprocess the current token.

关于javascript - "<tr>"的 parentNode 是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18526352/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com