我在控制台中执行以下几行
thead=document.createElement('thead')
thead.appendChild((document.createElement('th')).appendChild(document.createTextNode('Inner Text')))
但是当我执行 thead 时,返回的是一个以“Inner Text”为内容的“thead”标签。根据命令执行,没有“th”标签。
为什么不起作用?
最佳答案
.appendChild()
返回附加的 child :
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)
关于javascript - 在 appendchild 参数中创建的节点未执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22702777/