[英]javascript add data to table cell
<table border='1' id='output'>
<tr>
<td>
</td>
</tr>
</table>
我的JavaScript代码
document.getElementById("output").childNodes[0].childNodes[0].nodeValue = ajaxRequest.responseText;
不起作用请帮忙
document.getElementById("output").children[0].children[0].children[0].innerHTML;
您有两件事是错误的:
<table>
。 表必须具有<tbody>
标记。 这可能是浏览器添加的,这意味着您需要更深入一层来访问<td>
元素。
第二件事,对于非文本节点, nodeValue
始终为null,这就是<td>
。 而是使用innerHTML
属性更改元素的文本。
更正这两件事后,您的代码应如下所示:
document.getElementById("output").childNodes[0].childNodes[0].childNodes[0].innerHTML = ajaxRequest.responseText;
使用JQuery u可以轻松实现,如下所示:
$(document).ready(function(){
$("#output tr td").text("JQUERY HELP");
});
或者,如果您想继续使用javascript,则可以参考其他已发布的答案。
<table border='1' id='output'>
<tr>
<td></td>
</tr>
</table>
$(function(){
$('#output td').append("blaa");
});
希望这可以帮助。
亚历克斯是对的,有一个肢体TAg。 请尝试以下一种方法:document.getElementById(“ output”)。getElementsByTagName(“ td”)[0] .innerHTML =“ test1”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.