gpt4 book ai didi

Javascript appendChild() 调用在 IE 中不起作用

转载 作者:行者123 更新时间:2023-11-29 18:37:07 25 4
gpt4 key购买 nike

有人能解释一下为什么这段脚本不能在 IE 中运行吗?它似乎在 Firefox 中工作正常。我是 appendChild() API 的新手。

<html>
<head>
<script type='text/javascript'>

function makeTable()
{
nTable=document.createElement('table');
nTable.setAttribute('id','myTable');
nTable.setAttribute('border','1');

nRow1=document.createElement('tr');
nData11=document.createElement('td');
nData11.setAttribute('colspan','2');
nCenter11=document.createElement('center');
nBold=document.createElement('b');
nBold.appendChild(document.createTextNode('Title'));
nCenter11.appendChild(nBold);
nData11.appendChild(nCenter11);
nRow1.appendChild(nData11);

nRow2=document.createElement('tr');
nData21=document.createElement('td');
nCenter21=document.createElement('center');
nCenter21.appendChild(document.createTextNode('21'));
nData21.appendChild(nCenter21);
nData22=document.createElement('td');
nCenter22=document.createElement('center');
nCenter22.appendChild(document.createTextNode('22'));
nData22.appendChild(nCenter22);
nRow2.appendChild(nData21);
nRow2.appendChild(nData22);

nTable.appendChild(nRow1);
nTable.appendChild(nRow2);

alert('Almost there !');
try
{
document.getElementById('container').appendChild(nTable);
}
catch(e)
{
alert(e.message);
}
return;

}

</script>
</head>
<body>
<div id='container'>
</div>
<input type=button value='Go' onclick='makeTable();'>
</body>
</html>

最佳答案

参见 http://msdn.microsoft.com/en-us/library/ms532998(VS.85).aspx#TOM_DOM .与 Crescent Fresh 在他的评论中提到的类似,IE 需要将一个 tbody 元素插入到表格中,以便您可以使用 DOM:

Note Internet Explorer requires that you create a tBody element and insert it into the table when using the DOM. Since you are manipulating the document tree directly, Internet Explorer does not create the tBody, which is automatically implied when using HTML.

关于Javascript appendChild() 调用在 IE 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1894210/

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