我有一个 HTML 测试 block ,例如:
HTML 是一个字符串,而不是 DOM 元素,但我正在尝试查看是否有方法或方法可用于将字符串作为 DOM 插入,以便将其附加到 DOM .
var test='<tr class="rowHeaders">';
test=test+'<td id="sTD" name="sTD" width="4%">test.php</td>'
test=test+'<td width="2%"><input type="radio" name="tb" ></td>';
test=test+'<td id="tTD" name="tTD" width="2%">php</td>';
test=test+'<td width="2%"><input type="button" name="vv" ></td>';
test=test+'</tr>';
var scriptTBL=document.getElementById("scriptsT");
scriptTBL.children[0].appendChild(test);
尝试做这样的事情......
但是“test”不是有效的节点或元素,那么我如何将它添加到元素中呢??
我考虑过使用 innerHtml,但 DOM 的表/tbody 可能已经存在一个子项。
我一直在探索片段,但这不是点击!
测试 html 有 tbl:
<table id="scriptsT" name="scriptsT" >
<tr>
.
.
非常感谢您的指点或想法。
谢谢。
最佳答案
您可以附加到 innerHTML
:
scriptTBL.tBodies[0].innerHTML += test;
foo += bar
是 foo = foo + bar
的简写。您还可以通过这种方式简化您的 HTML 创建代码。使用 test += 'html here';
。
appendChild
只接受一个 DOM 元素。
关于javascript - 将字符串作为元素插入到 javascript 的 DOM 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5641652/