简体   繁体   English

html dom for td元素

[英]html dom for td element

How do I call the td element of a html table from the dom? 如何从dom调用html表的td元素? document.getElementById("exampleTable").childNodes[] Should give you all of the tr elements, but how do you go two levels down? document.getElementById(“ exampleTable”)。childNodes []应该为您提供所有tr元素,但是如何向下两层呢? I'm to make a loop to read the text node of each td element in the table 我要循环读取表中每个td元素的文本节点

document.getElementById("exampleTable").getElementsByTagName("td")

这将返回该表中所有td的NodeList。

Each of those child nodes will have child nodes too, so getting the childnodes of each TR, will give you all TDs. 这些子节点中的每个子节点也将具有子节点,因此获取每个TR的子节点将为您提供所有TD。

But if you're going to do DOM querying or DOM manipulation, I strongly suggest using JQuery. 但是,如果要进行DOM查询或DOM操作,我强烈建议使用JQuery。

you could do something like this 你可以做这样的事情

var int_tr_start = 0;

var arr_tr = []; // store all tr's

arr_tr =  document.getElementById("tab").getElementsByTagName('tr');

for (var TR = int_tr_start; TR < arr_tr.length; TR ++) { // for each td

  for (var TD = 0; TD < arr_tr[TR].childNodes.length; TD++) { //  for each tr
 var tc = (arr_tr.textContent) ? arr_tr[TR].childNodes[TD].textContent : arr_tr[TR].childNodes[TD].innerText; // store the text from with in cell
 alert(tc);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM