单击td里面的内容,变成文本框,是可以了,但是如果变成文本框后,再点击文本框外面的td(挨着),恢复到以前的数据后,还是会马上变成文本框。大家帮忙测试下,看是什么问题。。代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<script language="javascript">
//点击当前tr得到当前里面的数据
function clickAllInfo(trNum)
{
var tr=document.getElementById("tr"+trNum);
var vValue=new Array();
for(var j=0;j<tr.childNodes.length;j++)
{
//tr.childNodes[tdNum].innerHTML="<input type='text' value="+v+">";
vValue[j]=tr.childNodes[j].childNodes[0].nodeValue
}
return vValue;
}
//当文本框失去焦点时
function inputBlur(trNum,tdNum)
{
var t=document.getElementById("t");
clickAllInfo(trNum);
var tr=document.getElementById("tr"+trNum);
tr.childNodes[tdNum].innerHTML=t.value;
}
//当文本框获得焦点时
function inputFocus(trNum,tdNum)
{
var tr=document.getElementById("tr"+trNum);
//tr.childNodes[tdNum].innerHTML="<input type='text' value=33 onblur='inputBlur("+trNum+","+tdNum+")' id='t' onclick='inputFocus("+trNum+","+tdNum+")'>"
document.getElementById("t").focus();
}
function clickInfo(trNum,tdNum)
{
var tr=document.getElementById("tr"+trNum);
var v=tr.childNodes[tdNum].childNodes[0].nodeValue;
var vValue=clickAllInfo(trNum);
//alert("22")
for(var j=0;j<tr.childNodes.length;j++)
{
tr.childNodes[j].innerHTML=vValue[j];
}
tr.childNodes[tdNum].innerHTML="<input type='text' value="+v+" onblur='inputBlur("+trNum+","+tdNum+")' id='t' onclick='stopBubble()'>"
document.getElementById("t").focus();
}
//阻止事件冒泡
function stopBubble(e) {
if ( e && e.stopPropagation ) e.stopPropagation();
else window.event.cancelBubble = true;
}</script>
<body>
<table width="97%" border="1" align="center">
<tr>
<td height="25" colspan="5" align="center">AJAX管理</td>
</tr>
<tr>
<td height="25" align="center">新闻ID</td>
<td height="25" align="center">新闻标题</td>
<td height="25" align="center">新闻作者</td>
<td height="25" align="center">新闻来源</td>
<td height="25" align="center">操作</td>
</tr>
<tr id="tr1">
<td height="25" align="center" id="td0" onclick="clickInfo(1,0)">1</td>
<td height="25" align="center" id="td1" onclick="clickInfo(1,1)">测试哦</td>
<td height="25" align="center" id="td2" onclick="clickInfo(1,2)">匿名</td>
<td height="25" align="center" id="td3" onclick="clickInfo(1,3)">恩施晚报</td>
<td height="25" align="center"> </td>
</tr>
<tr id="tr2">
<td height="25" align="center" id="td0">2</td>
<td height="25" align="center" id="td1">下雨</td>
<td height="25" align="center" id="td2">admin</td>
<td height="25" align="center" id="td3">百度</td>
<td height="25" align="center"> </td>
</tr>
</table>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<script language="javascript">
//点击当前tr得到当前里面的数据
function clickAllInfo(trNum)
{
var tr=document.getElementById("tr"+trNum);
var vValue=new Array();
for(var j=0;j<tr.childNodes.length;j++)
{
//tr.childNodes[tdNum].innerHTML="<input type='text' value="+v+">";
vValue[j]=tr.childNodes[j].childNodes[0].nodeValue
}
return vValue;
}
//当文本框失去焦点时
function inputBlur(trNum,tdNum)
{
var t=document.getElementById("t");
clickAllInfo(trNum);
var tr=document.getElementById("tr"+trNum);
tr.childNodes[tdNum].innerHTML=t.value;
}
//当文本框获得焦点时
function inputFocus(trNum,tdNum)
{
var tr=document.getElementById("tr"+trNum);
//tr.childNodes[tdNum].innerHTML="<input type='text' value=33 onblur='inputBlur("+trNum+","+tdNum+")' id='t' onclick='inputFocus("+trNum+","+tdNum+")'>"
document.getElementById("t").focus();
}
function clickInfo(trNum,tdNum)
{
var tr=document.getElementById("tr"+trNum);
var v=tr.childNodes[tdNum].childNodes[0].nodeValue;
var vValue=clickAllInfo(trNum);
//alert("22")
for(var j=0;j<tr.childNodes.length;j++)
{
tr.childNodes[j].innerHTML=vValue[j];
}
tr.childNodes[tdNum].innerHTML="<input type='text' value="+v+" onblur='inputBlur("+trNum+","+tdNum+")' id='t' onclick='stopBubble()'>"
document.getElementById("t").focus();
}
//阻止事件冒泡
function stopBubble(e) {
if ( e && e.stopPropagation ) e.stopPropagation();
else window.event.cancelBubble = true;
}</script>
<body>
<table width="97%" border="1" align="center">
<tr>
<td height="25" colspan="5" align="center">AJAX管理</td>
</tr>
<tr>
<td height="25" align="center">新闻ID</td>
<td height="25" align="center">新闻标题</td>
<td height="25" align="center">新闻作者</td>
<td height="25" align="center">新闻来源</td>
<td height="25" align="center">操作</td>
</tr>
<tr id="tr1">
<td height="25" align="center" id="td0" onclick="clickInfo(1,0)">1</td>
<td height="25" align="center" id="td1" onclick="clickInfo(1,1)">测试哦</td>
<td height="25" align="center" id="td2" onclick="clickInfo(1,2)">匿名</td>
<td height="25" align="center" id="td3" onclick="clickInfo(1,3)">恩施晚报</td>
<td height="25" align="center"> </td>
</tr>
<tr id="tr2">
<td height="25" align="center" id="td0">2</td>
<td height="25" align="center" id="td1">下雨</td>
<td height="25" align="center" id="td2">admin</td>
<td height="25" align="center" id="td3">百度</td>
<td height="25" align="center"> </td>
</tr>
</table>
</body>
</html>
实在不行的话,就改成双击单元格编辑。
<td height="25" align="center" id="td0" ondblclick="clickInfo(1,0)">1</td>
<td height="25" align="center" id="td1" ondblclick="clickInfo(1,1)">测试哦</td>
<td height="25" align="center" id="td2" ondblclick="clickInfo(1,2)">匿名</td>
<td height="25" align="center" id="td3" ondblclick="clickInfo(1,3)">恩施晚报</td>
<td height="25" align="center"> </td>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<script language="javascript">
var pre;
function clickInfo(th){
if(pre==null){
pre=th;
th.isText=true;
th.innerHTML="<input type='text' value="+th.innerHTML+" id='t' onclick='stopBubble()'>";
}else if(pre!=th){
if(document.getElementById("t")!=null){
pre.innerHTML=document.getElementById("t").value;
pre.isText=false;
}
th.isText=!th.isText;
th.innerHTML="<input type='text' value="+th.innerHTML+" id='t' onclick='stopBubble()'>";
pre=th;
}else{
if(th.isText==undefined||!th.isText){
th.isText=true;
//alert(th.innerHTML);
th.innerHTML="<input type='text' value="+th.innerHTML+" id='t' onclick='stopBubble()'>";
}else{
th.innerHTML=document.getElementById("t").value;
th.isText=!th.isText;
}
}
pre=th;
}function stopBubble(e) {
if ( e && e.stopPropagation ) e.stopPropagation();
else window.event.cancelBubble = true;
}
</script>
<body>
<table width="97%" border="1" align="center">
<tr>
<td height="25" colspan="5" align="center">AJAX管理</td>
</tr>
<tr>
<td height="25" align="center">新闻ID</td>
<td height="25" align="center">新闻标题</td>
<td height="25" align="center">新闻作者</td>
<td height="25" align="center">新闻来源</td>
<td height="25" align="center">操作</td>
</tr>
<tr id="tr1">
<td height="25" align="center" id="td0" onclick="clickInfo(this)">1</td>
<td height="25" align="center" id="td1" onclick="clickInfo(this)">测试哦</td>
<td height="25" align="center" id="td2" onclick="clickInfo(this)">匿名</td>
<td height="25" align="center" id="td3" onclick="clickInfo(this)">恩施晚报</td>
<td height="25" align="center"> </td>
</tr>
<tr id="tr2">
<td height="25" align="center" id="td0">2</td>
<td height="25" align="center" id="td1">下雨</td>
<td height="25" align="center" id="td2">admin</td>
<td height="25" align="center" id="td3">百度</td>
<td height="25" align="center"> </td>
</tr>
</table>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<script language="javascript">
function inputBlur()
{ document.getElementById("t").parentNode.innerHTML=document.getElementById("t").value;
//alert(document.getElementById("t").value);
//alert(document.getElementById("t").parentNode.id)
}
var pre;
function clickInfo(th){
if(pre==null){
pre=th;
th.isText=true;
th.innerHTML="<input type='text' value="+th.innerHTML+" id='t' onclick='stopBubble()' onBlur='inputBlur()'>";
document.getElementById("t").focus();
}else if(pre!=th){
if(document.getElementById("t")!=null){
pre.innerHTML=document.getElementById("t").value;
pre.isText=false;
}
th.isText=!th.isText;
th.innerHTML="<input type='text' value="+th.innerHTML+" id='t' onclick='stopBubble()' onBlur='inputBlur()'>";
document.getElementById("t").focus();
pre=th;
}else{
if(th.isText==undefined||!th.isText){
th.isText=true;
//alert(th.innerHTML);
th.innerHTML="<input type='text' value="+th.innerHTML+" id='t' onclick='stopBubble()' onBlur='inputBlur()'>";
document.getElementById("t").focus();
}else{
th.innerHTML=document.getElementById("t").value;
th.isText=!th.isText;
}
}
pre=th;
}function stopBubble(e) {
if ( e && e.stopPropagation ) e.stopPropagation();
else window.event.cancelBubble = true;
}
</script>
<body>
<table width="97%" border="1" align="center">
<tr>
<td height="25" colspan="5" align="center">AJAX管理</td>
</tr>
<tr>
<td height="25" align="center">新闻ID</td>
<td height="25" align="center">新闻标题</td>
<td height="25" align="center">新闻作者</td>
<td height="25" align="center">新闻来源</td>
<td height="25" align="center">操作</td>
</tr>
<tr id="tr1">
<td height="25" align="center" id="td0" onclick="clickInfo(this)">1</td>
<td height="25" align="center" id="td1" onclick="clickInfo(this)">测试哦</td>
<td height="25" align="center" id="td2" onclick="clickInfo(this)">匿名</td>
<td height="25" align="center" id="td3" onclick="clickInfo(this)">恩施晚报</td>
<td height="25" align="center"> </td>
</tr>
<tr id="tr2">
<td height="25" align="center" id="td0">2</td>
<td height="25" align="center" id="td1">下雨</td>
<td height="25" align="center" id="td2">admin</td>
<td height="25" align="center" id="td3">百度</td>
<td height="25" align="center"> </td>
</tr>
</table>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312" />
<title>无标题文档</title>
</head>
<script language="javascript">
var pre,tem=false;
function clickInfo(th){
if(pre==null){
pre=th;
th.isText=true;
th.innerHTML="<input type='text' value="+th.innerHTML+" onblur='bl(this)' id='t' onclick='stopBubble()'>";
document.getElementById("t").focus();
}else if(pre!=th){
if(document.getElementById("t")!=null){
pre.innerHTML=document.getElementById("t").value;
pre.isText=false;
}
th.isText=!th.isText;
th.innerHTML="<input type='text' value="+th.innerHTML+" onblur='bl(this)' id='t' onclick='stopBubble()'>";
document.getElementById("t").focus();
pre=th;
}else{
if(th.isText==undefined||!th.isText){
th.isText=true;
th.innerHTML="<input type='text' value="+th.innerHTML+" onblur='bl(this)' id='t' onclick='stopBubble()'>";
document.getElementById("t").focus();
}else{
th.innerHTML=document.getElementById("t").value;
th.isText=!th.isText;
}
}
pre=th;
}
function mouover(){
tem=true;
}
function mouout(){
tem=false;
}
function bl(){
if(!tem){
pre.isText=false;
pre.innerHTML=document.getElementById("t").value;
}
}function stopBubble(e) {
if ( e && e.stopPropagation ) e.stopPropagation();
else window.event.cancelBubble = true;
}
</script>
<body>
<table width="97%" border="1" align="center">
<tr>
<td height="25" colspan="5" align="center">AJAX管理</td>
</tr>
<tr>
<td height="25" align="center">新闻ID</td>
<td height="25" align="center">新闻标题</td>
<td height="25" align="center">新闻作者</td>
<td height="25" align="center">新闻来源</td>
<td height="25" align="center">操作</td>
</tr>
<tr id="tr1">
<td height="25" align="center" id="td0" onclick="clickInfo(this)" onmouseover="mouover()" onmouseout="mouout()">1</td>
<td height="25" align="center" id="td1" onclick="clickInfo(this)" onmouseover="mouover()" onmouseout="mouout()">测试哦</td>
<td height="25" align="center" id="td2" onclick="clickInfo(this)" onmouseover="mouover()" onmouseout="mouout()">匿名</td>
<td height="25" align="center" id="td3" onclick="clickInfo(this)" onmouseover="mouover()" onmouseout="mouout()">恩施晚报</td>
<td height="25" align="center"> </td>
</tr>
<tr id="tr2">
<td height="25" align="center" id="td0">2</td>
<td height="25" align="center" id="td1">下雨</td>
<td height="25" align="center" id="td2">admin</td>
<td height="25" align="center" id="td3">百度</td>
<td height="25" align="center"> </td>
</tr>
</table>
</body>
</html>
function inputBlur()
{document.getElementById("t").parentNode.innerHTML=document.getElementById("t").value;
//alert(document.getElementById("t").value);
//alert(document.getElementById("t").parentNode.id)
}
var pre;
function clickInfo(th) {
if (pre == null || pre != th) {
pre = th;
th.isText = true;
th.innerHTML = "<input type='text' value=" + th.innerHTML + " id='t' onclick='stopBubble()' onblur='inputBlur()'>";
document.getElementById('t').focus();
}
}
function inputBlur() {
document.getElementById('t').parentNode.innerHTML = document.getElementById('t').value;
}
</script>
你在16楼的代码运行报错是因为没有维护好pre.isText属性:
function inputBlur() {
pre.isText = false; //加上这一句就可以运行,但是没有解决再次点击同一个单元格的问题
document.getElementById("t").parentNode.innerHTML=document.getElementById("t").value;
}