Javascript 在appendchild参数中创建的节点未执行

Javascript 在appendchild参数中创建的节点未执行,javascript,Javascript,我在控制台中执行以下行 thead=document.createElement('thead') thead.appendChild((document.createElement('th')).appendChild(document.createTextNode('Inner Text'))) 但当我执行thead时,返回的是一个“thead”标记,其内容是“Inner Text”。根据命令执行,没有“th”标记 为什么它不起作用 node.appendChild返回附加的元素 appe

我在控制台中执行以下行

thead=document.createElement('thead')
thead.appendChild((document.createElement('th')).appendChild(document.createTextNode('Inner Text')))
但当我执行thead时,返回的是一个“thead”标记,其内容是“Inner Text”。根据命令执行,没有“th”标记

为什么它不起作用

node.appendChild返回附加的元素

appendChild方法返回对添加节点的引用

你会想把它分解成这样

var thead = document.createElement('thead');
var th = document.createElement('th');
thead.appendChild(th);
th.appendChild(document.createTextNode('Inner Text')));
node.appendChild返回追加的元素

appendChild方法返回对添加节点的引用

你会想把它分解成这样

var thead = document.createElement('thead');
var th = document.createElement('th');
thead.appendChild(th);
th.appendChild(document.createTextNode('Inner Text')));
.appendChild返回附加的子项:

var thead=document.createElement('thead');
var th = document.createElement('th');
th.appendChild(document.createTextNode('Inner Text'));
thead.appendChild(th);
如果要在一行中执行此操作,可以调用添加节点的.parentNode:

thead.appendChild(document.createElement('th').appendChild(document.createTextNode('Inner Text')).parentNode)
.appendChild返回附加的子项:

var thead=document.createElement('thead');
var th = document.createElement('th');
th.appendChild(document.createTextNode('Inner Text'));
thead.appendChild(th);
如果要在一行中执行此操作,可以调用添加节点的.parentNode:

thead.appendChild(document.createElement('th').appendChild(document.createTextNode('Inner Text')).parentNode)

根据,它是正确的。@FelixKling我先写错了,现在应该是正确的。根据,它是正确的。@FelixKling我先写错了,现在应该是正确的。如果你在将thead附加到文档之前这样做,不要太担心附加顺序,因为它不会被绘制是的,只要删除before createElement'th',在队伍的最后。但你为什么一开始就把th放在THAD里?它属于tr。如果您在将ad附加到文档之前执行此操作,则不必担心附加顺序,因为它不会被绘制,只需删除before createElement“th”和行末尾的。但你为什么一开始就把th放在THAD里?它属于tr。您的最后一行将与问题中的代码相同。@cookiemonster不,不是,请在行尾检查它。对不起,问题中的参数看起来真的很混乱,因为document.createElementth周围有额外的内容。更干净的解决方案是ad.appendChilddocument.createElement'th'。appendChilddocument.createTextNode'Inner Text'不需要.parentNode。只需更正问题定义的顺序。您的最后一行将与问题中的代码相同。@cookiemonster不,不是,请在行末检查它。抱歉,问题中的参数看起来真的很混乱,因为额外的document.createElementth。更干净的解决方案是ad.appendChilddocument.createElement'th'。appendChilddocument.createTextNode'Inner Text'不需要.parentNode。只需要纠正问题定义的顺序。