初始化之后,i=1,所以onclick的时候tds[i]=tds[1]这样就可以了:
function init(){
var tables = document.getElementById("table1");
var tds = tables.getElementsByTagName("td");
//alert(tds.length);
for(var i = 0 ; i < tds.length ; i++){
//alert(tds[i]);
tds[i].onclick=function(){
this.childNodes[0].style.display = "none";
this.childNodes[2].style.display = "block";
//alert(1);
}
tds[i].childNodes[2].onchange =function(){
this.parentNode.childNodes[0].style.display = "block";
this.parentNode.childNodes[2].style.display = "none";
this.parentNode.childNodes[0].innerHTML = this.parentNode.childNodes[2].value;
}
}
}
function init(){
var tables = document.getElementById("table1");
var tds = tables.getElementsByTagName("td");
//alert(tds.length);
for(var i = 0 ; i < tds.length ; i++){
//alert(tds[i]);
tds[i].onclick=function(){
this.childNodes[0].style.display = "none";
this.childNodes[2].style.display = "block";
//alert(1);
}
tds[i].childNodes[2].onchange =function(){
this.parentNode.childNodes[0].style.display = "block";
this.parentNode.childNodes[2].style.display = "none";
this.parentNode.childNodes[0].innerHTML = this.parentNode.childNodes[2].value;
}
}
}
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<style>
input{
border:0px solid #ffffff;
background-color:#ffffcc;
}
</style>
<body>
<table border="1" width="50%" id="table1">
<tr>
<td>
<span id="userName1Span" >test</span>
<input type="text" name="userName1" value="test" style="display:none"/>
</td>
</tr>
<tr>
<td>
<span id="userName2Span" >test</span>
<input type="text" name="userName2" value="test" style="display:none"/>
</td>
</tr>
</table>
</body>
</html>
<script language="javascript">
function init()
{
var table = document.getElementById("table1");
var tds = table.getElementsByTagName("td");
for(var i = 0 ; i < tds.length ;i++)
{
//note = tds[i].children;
//alert(tds[i].children.length);
tds[i].onclick = function()
{
if(this.children[0].id.indexOf("userName") != -1)
{
this.children[0].style.display = "none";
this.children[1].style.display = "";
}
}
tds[i].children[1].onblur = function()
{
this.parentElement.children[0].style.display = "";
this.parentElement.children[1].style.display = "none";
this.parentElement.children[0].innerText = this.parentElement.children[1].value;
}
}
}
window.onload = init;
</script>
tds[i].childNodes[2].onchange =function(){
这里似乎没有3个子节点的
最多就是到tds[i].childNodes[1]了吧