Javascript,如何访问一行的特定子项?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3840342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript, how can I access a specific child of a row?
提问by robert
Using Javascript, how can I access a specific child of a row? Javascript (not jQuery please).
使用 Javascript,如何访问一行的特定子项?Javascript(请不要使用 jQuery)。
e.g.: second <TD>
of <TR>
where ID=id33322010100167
例如:第二个<TD>
,<TR>
其中 ID=id33322010100167
<table>
<tr id="id33322010100167">
<td>20101001</td>
<td>918</td>
<td>919</td>
<td>67</td>
<td>CAR PROBLEM</td>
</tr>
<tr id="id33322010100169">
<td>20102001</td>
<td>913</td>
<td>914</td>
<td>62</td>
<td>LUNCHTIME</td>
</tr>
<table>
采纳答案by RaYell
var index = 1; // second element
var child = document.getElementById('id33322010100167').childNodes[index]
回答by Shaoz
Maybe you can try this:
也许你可以试试这个:
var tRow = document.getElementById("tableName").getElementsByTagName("tr");
for(var i = 0; i < tRow.length; i++){
if(tRow[i].id == "name of your id"){
//do something
}
}
回答by Chris Sagined
I found this solution in combining the two most rated one:
我在结合两个最受好评的解决方案时找到了这个解决方案:
Instead of using childNodes you could get Elements of typ td below this row with:
您可以使用以下命令在此行下方获得 typ td 元素,而不是使用 childNodes:
var array_td = document.getElementById('id33322010100167').getElementsByTagName("td");
To get the content of the first td you could use:
要获取第一个 td 的内容,您可以使用:
array_td[1]
The main benefit of this is, that you do not have all the whitespace you dont want to handle inside the array and you dont have to change the index if some new childs (not tds) be added.
这样做的主要好处是,您没有不想在数组内处理的所有空白,并且如果添加了一些新的子项(不是 tds),则不必更改索引。
回答by Tim Down
Most reliable is the cells
collection, which unlike childNodes
in non-IE browsers will ignore whitespace text nodes between cells:
最可靠的是cells
集合,与childNodes
非 IE 浏览器不同,它会忽略单元格之间的空白文本节点:
var td = document.getElementById("id33322010100167").cells[1];
回答by Dan Iveson
Using the DOM you can get the table and then iterate over the child elements while keeping count.
使用 DOM,您可以获得表格,然后在保持计数的同时迭代子元素。
Of course element ids are supposed to be unique so that document.getElementById('id')
works.
当然,元素 id 应该是唯一的,这样才document.getElementById('id')
有效。
回答by Thariama
Use jQuery:
使用jQuery:
first = $('#id33322010100167 td:first'); // gives you the first td of the tr with id id33322010100167
last = $('#id33322010100167 td:last'); // gives you the last td of the tr with id id33322010100167
you may use next() to iterate through the elements
您可以使用 next() 来遍历元素
first.next();
回答by Ishara Madawa
<table id="studTable" class="table table-bordered">
<thead>
<tr>
<td>Click</td>
</tr>
</thead>
<tbody>
<tr><td><a href="#" onclick="ClickRow(this)">Click</a></td></tr>
</tbody>
</table>
for above table you can use something link this. you can read your selected row by clicking row/cell inside row and read cell values and/or remove row from table
对于上表,您可以使用链接此内容。您可以通过单击行内的行/单元格读取所选行并读取单元格值和/或从表中删除行
function ClickRow(r) {
console.log(r.parentNode.parentNode.cells[0].innerHTML); //Read First Cell in Row
var i = r.parentNode.parentNode.rowIndex;
document.getElementById("studTable").deleteRow(i); //Remove Row
}