I seem 开发者_JAVA百科to be getting different results of fromDate between Mozilla and IE. Is there a more reliable cross-browser way of doing this? [edit] I am actually getting different column values! [/edit]
<tr id='id3332201010241' />
<td>20101024</td>
<td>20101025</td>
<td>1415</td>
<td>1445</td>
<td>INVDROP</td>
<td>H4T1A3</td>
<td><a href='#' onclick='selectEditActivity("id3332201010241");' >Click Here</a></td>
</tr>
function selectEditActivity(pass_id){
row = document.getElementById(pass_id);
var seq = document.getElementById(pass_id).getAttribute("seq");
var from_date = row.childNodes[1].innerHTML;
alert(from_date);
var to_date = row.childNodes[3].innerHTML;
}
You wrongly made <tr>
a self-closing tag
<tr id='id3332201010241' />
should be
<tr id='id3332201010241'>
Another thing is that childNodes
may not work as you expect since it will contain text nodes. If you want to get <td>
elements in a row you should use:
var row = document.getElementById(pass_id);
var columns = row.getElementsByTagName("td");
var from_date = columns[1].innerHTML;
var to_date = columns[3].innerHTML;
The childNodes nodeList consists of all child nodes of the element, including (empty) text nodes and comment nodes. @quirksmode
You are seeing the result of differences in how different browsers handle white-space. Instead (for more general cases) you can use getElementsByTagName()
to for sure get a certain type of child element (<td>
in this case), like this:
function selectEditActivity(pass_id){
var row = document.getElementById(pass_id),
cells = row.getElementsByTagName("td"),
seq = row.getAttribute("seq"),
from_date = cells[0].innerHTML,
to_date = cells[1].innerHTML;
alert("From: " + from_date + "\nTo: " + to_date);
}
You can test it out here. As @patrick points out though, it's not needed here, just use the .cells
of the <tr>
, like this:
function selectEditActivity(pass_id){
var row = document.getElementById(pass_id),
seq = row.getAttribute("seq"),
from_date = row.cells[0].innerHTML,
to_date = row.cells[1].innerHTML;
alert("From: " + from_date + "\nTo: " + to_date);
}
You can test it out here.
精彩评论