---
title: "JavaScript实现在线编辑表"
author: "aa00aa00"
date: "2009-12-10T11:21:13Z"
tags: ["CSS", "JavaScript"]
categories: ["编程语言"]
slug: "javascript"
draft: false
lastmod: "2009-12-10T11:21:13Z"
id: 545345
url: "https://aa00aa00.iteye.com/blog/545345"
---

在JavaScript版经常看到有人关于在线可编辑表格的问题，于是整理了出来。

主要功能：

1.动态增加行

2.可以点击表格左边的按钮删除当前行，如果该行有数据会要求用户确认删除

3.表格可以实现动态编辑

4.通过点击某一行可以实现高亮度显示

源代码：

＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝

&lt;HTML&gt;  
&lt;HEAD&gt;  
&lt;META content="text/html; charset=gb2312" http-equiv=Content-Type&gt;  
&lt;STYLE type=text/css&gt;BODY {  
BACKGROUND: white; COLOR: black; FONT-FAMILY: verdana,arial,helvetica; FONT-SIZE: 12px  
}  
.tableData {  
BACKGROUND: white; BORDER-BOTTOM: white 1px dashed; BORDER-LEFT: white 1px dashed; BORDER-RIGHT: white 1px dashed; BORDER-TOP: white 1px dashed; COLOR: black; CURSOR: hand; FONT-FAMILY: verdana,arial,helvetica; FONT-SIZE: 12px  
}  
.tableDataSel {  
BACKGROUND: #6090d0; BORDER-BOTTOM: #6090d0 1px dashed; BORDER-LEFT: #6090d0 1px dashed; BORDER-RIGHT: #6090d0 1px dashed; BORDER-TOP: #6090d0 1px dashed; COLOR: white; CURSOR: hand; FONT-FAMILY: verdana,arial,helvetica; FONT-SIZE: 12px  
}  
.tableDataHit {  
BACKGROUND: #d0e0ff; BORDER-BOTTOM: #d0e0ff 1px dashed; BORDER-LEFT: #d0e0ff 1px dashed; BORDER-RIGHT: #d0e0ff 1px dashed; BORDER-TOP: #d0e0ff 1px dashed; COLOR: black; CURSOR: hand; FONT-FAMILY: verdana,arial,helvetica; FONT-SIZE: 12px  
}  
.tableDataOver {  
BACKGROUND: #d0e0ff; BORDER-BOTTOM: #d0e0ff 1px dashed; BORDER-LEFT: #d0e0ff 1px dashed; BORDER-RIGHT: #d0e0ff 1px dashed; BORDER-TOP: #d0e0ff 1px dashed; COLOR: black; CURSOR: hand; FONT-FAMILY: verdana,arial,helvetica; FONT-SIZE: 12px  
}  
.controlPanel {  
BACKGROUND: silver; COLOR: black; CURSOR: hand; FONT-FAMILY: verdana,arial,helvetica; FONT-SIZE: 14px  
}  
.controlPanelMouseOver {  
BACKGROUND: #d0e0ff; COLOR: black; CURSOR: hand; FONT-FAMILY: verdana,arial,helvetica; FONT-SIZE: 14px  
}  
.delMouseOver {  
BACKGROUND: #d0e0ff; COLOR: black; CURSOR: hand; FONT-FAMILY: verdana,arial,helvetica; FONT-SIZE: 14px  
}  
.scheduleButtonVisible {  
BACKGROUND: silver; BORDER-BOTTOM: gray 1px dashed; BORDER-LEFT: white 1px dashed; BORDER-RIGHT: gray 1px dashed; BORDER-TOP: white 1px dashed; COLOR: black; CURSOR: hand; FONT-FAMILY: webdings; FONT-SIZE: 12px  
}  
&lt;/STYLE&gt;  
&lt;SCRIPT language=javascript&gt;  
var modified=0  
var currentLine=-1//该变量似乎保存"ln"属性；lightOn函数中line记录实际行数  
var line=-1;  
function lightOn(ln)  
{  
if (ln==null)  
  ln=parseInt(event.srcElement.ln,10)  
for (i=1;i&lt;PLList.rows.length;i++)  
  if (tdt\[i].ln==ln)  
   line=i  
cln=currentLine  
oldLine=-1  
for (i=1;i&lt;PLList.rows.length;i++)  
  if (tdt\[i].ln==cln)  
   oldLine=i  
if (line==oldLine)  
  return  
//if td &lt;0 then only clear all title  
if (line&gt;0)  
{  
  document.all.tdt\[line].className="tableDataHit";  
  document.ecform("date")\[line].className="tableDataHit";  
  document.ecform("diagnosis")\[line].className="tableDataHit";  
  document.ecform("medication")\[line].className="tableDataHit";  
  if (oldLine&gt;0)  
  {  
   document.all.tdt\[oldLine].className="tableData";  
   document.ecform("date")\[oldLine].className="tableData";  
   document.ecform("diagnosis")\[oldLine].className="tableData";  
   document.ecform("medication")\[oldLine].className="tableData";  
  }  
  currentLine=ln  
}  
}  
function delIt()  
{  
line=parseInt(event.srcElement.ln,10)  
   
if (line&gt;0)  
  for (i=1;i&lt;PLList.rows.length;i++)  
   if (tdt\[i].ln==line)  
   {  
    if (document.ecform.diagnosis\[i].value.length&gt;0 || document.ecform.medication\[i].value.length&gt;0 || document.ecform.date\[i].value.length&gt;0)  
     if (!confirm("Are you sure you want to delete this record?"))  
      return  
    PLList.deleteRow(i)  
   }  
}  
function modifiedIt(field)  
{  
// field.value=checkText(field.value);  
modified=1  
line=parseInt(event.srcElement.ln,10)  
   
if (line&gt;0)  
   for (i=1;i&lt;PLList.rows.length;i++)  
    if (tdt\[i].ln==line)  
     document.ecform.edit\[i].value=1  
}  
function newHospitalVisit()  
{  
newRow=PLList.insertRow(PLList.rows.length);  
newRow.id="tdt";  
newRow.ln=allCount;  
newRow.bgColor="#e0e0e0";  
newRow.className="tableData";  
newRow.onclick=lightOn;

c1=newRow.insertCell(0);  
c1.id="delItem";  
c1.ln=allCount;  
c1.className="scheduleButtonVisible";  
c1.onclick=delIt;  
c1.onmouseover=mouseOver;  
  c1.onmouseout=mouseOut;  
c1.innerHTML="&lt;input type=hidden name=PLID value=0&gt;&lt;input type='hidden' name='edit' value=0&gt;";  
c2=newRow.insertCell(1);  
c2.ln=allCount;  
c2.innerHTML="&lt;input type=text size=10 maxlength=10 name=date ln="+allCount+" class=tableData onkeyup='modifiedIt()'&gt;"  
c3=newRow.insertCell(2);  
c3.ln=allCount;  
c3.innerHTML="&lt;input type=text size=25 maxlength=50 name=diagnosis ln="+allCount+" class=tableData onkeyup='modifiedIt()'&gt;"  
c4=newRow.insertCell(3);  
c4.ln=allCount;  
c4.innerHTML="&lt;input type=text size=58 maxlength=100 name=medication ln="+allCount+" class=tableData onkeyup='modifiedIt()'&gt;"  
allCount++  
}  
function mouseOver()  
{  
event.srcElement.className="delMouseOver"  
}  
function mouseOut()  
{  
event.srcElement.className="scheduleButtonVisible"  
}  
&lt;/SCRIPT&gt;  
&lt;/HEAD&gt;  
&lt;BODY bgColor=white&gt;  
&lt;FORM method=post name=ecform&gt;  
  &lt;META content="MSHTML 5.00.2920.0" name=GENERATOR&gt;  
  &lt;DIV id=div1  
style="LEFT: 0px; POSITION: absolute; TOP: 0px; VISIBILITY: visible; WIDTH: 658px"&gt;  
    &lt;TABLE bgColor=silver border=2 borderColorDark=gray borderColorLight=silver  
cellPadding=2 cellSpacing=1 cols=1 id=PLList rules=rows width="100%"&gt;  
      &lt;TBODY&gt;  
      &lt;TR bgColor=#0a6846 id=tdt&gt;  
        &lt;TH width=10&gt;&lt;FONT color=white&gt;X  
          &lt;INPUT name=PLID type=hidden&gt;  
          &lt;INPUT  
      name=edit type=hidden&gt;  
          &lt;/FONT&gt;&lt;/TH&gt;  
        &lt;TH width="10%"&gt;  
          &lt;INPUT name=date type=hidden&gt;  
          &lt;FONT color=white&gt;Date &lt;/FONT&gt;  
        &lt;TH width="30%"&gt;  
          &lt;INPUT name=diagnosis type=hidden&gt;  
          &lt;FONT  
      color=white&gt;Diagnosis &lt;/FONT&gt;  
        &lt;TH width="60%"&gt;  
          &lt;INPUT name=medication type=hidden&gt;  
          &lt;FONT  
      color=white&gt;Medication &lt;/font&gt;  
      &lt;/TR&gt;  
      &lt;TR bgColor=#e0e0e0 class=tableData id=tdt onclick=lightOn() ln="1"&gt;  
        &lt;TD class=scheduleButtonVisible onclick=delIt() onmouseout=mouseOut()  
    onmouseover=mouseOver() ln="1"&gt;&lt;FONT size=2&gt;  
          &lt;INPUT name=PLID type=hidden  
      value=54 ln="1"&gt;  
          &lt;INPUT name=edit type=hidden value=0&gt;  
          &lt;/FONT&gt;&lt;/TD&gt;  
        &lt;TD ln="1"&gt;  
          &lt;INPUT class=tableData maxLength=10 name=date  
      onkeyup=modifiedIt(this) size=10 ln="1"&gt;  
        &lt;/TD&gt;  
        &lt;TD ln="1"&gt;  
          &lt;INPUT class=tableData maxLength=50 name=diagnosis  
      onkeyup=modifiedIt(this) size=25 value="what do you think of it?"  
    ln="1"&gt;  
        &lt;/TD&gt;  
        &lt;TD ln="1"&gt;  
          &lt;INPUT class=tableData maxLength=100 name=medication  
      onkeyup=modifiedIt(this) size=58 value="like it?" ln="1"&gt;  
        &lt;/TD&gt;  
      &lt;/TR&gt;  
      &lt;TR bgColor=#e0e0e0 class=tableData id=tdt onclick=lightOn() ln="2"&gt;  
        &lt;TD class=scheduleButtonVisible onclick=delIt() onmouseout=mouseOut()  
    onmouseover=mouseOver() ln="2"&gt;&lt;FONT size=2&gt;  
          &lt;INPUT name=PLID type=hidden  
      value=55 ln="2"&gt;  
          &lt;INPUT name=edit type=hidden value=0&gt;  
          &lt;/FONT&gt;&lt;/TD&gt;  
        &lt;TD ln="2"&gt;  
          &lt;INPUT class=tableData maxLength=10 name=date  
      onkeyup=modifiedIt(this) size=10 value=01/01/1999 ln="2"&gt;  
        &lt;/TD&gt;  
        &lt;TD ln="2"&gt;  
          &lt;INPUT class=tableData maxLength=50 name=diagnosis  
      onkeyup=modifiedIt(this) size=25 value="it\`s a test" ln="2"&gt;  
        &lt;/TD&gt;  
        &lt;TD ln="2"&gt;  
          &lt;INPUT class=tableData maxLength=100 name=medication  
      onkeyup=modifiedIt(this) size=58 value="how about it?" ln="2"&gt;  
        &lt;/TD&gt;  
      &lt;/TR&gt;  
      &lt;TR bgColor=#e0e0e0 class=tableData id=tdt onclick=lightOn() ln="3"&gt;  
        &lt;TD class=scheduleButtonVisible onclick=delIt() onmouseout=mouseOut()  
    onmouseover=mouseOver() ln="3"&gt;&lt;FONT size=2&gt;  
          &lt;INPUT name=PLID type=hidden  
      value=56 ln="3"&gt;  
          &lt;INPUT name=edit type=hidden value=0&gt;  
          &lt;/FONT&gt;&lt;/TD&gt;  
        &lt;TD ln="3"&gt;  
          &lt;INPUT class=tableData maxLength=10 name=date  
      onkeyup=modifiedIt(this) size=10 value=02/01/2000 ln="3"&gt;  
        &lt;/TD&gt;  
        &lt;TD ln="3"&gt;  
          &lt;INPUT class=tableData maxLength=50 name=diagnosis  
      onkeyup=modifiedIt(this) size=25 value="another test" ln="3"&gt;  
        &lt;/TD&gt;  
        &lt;TD ln="3"&gt;  
          &lt;INPUT class=tableData maxLength=100 name=medication  
      onkeyup=modifiedIt(this) size=58 ln="3"&gt;  
        &lt;/TD&gt;  
      &lt;/TR&gt;  
      &lt;/TBODY&gt;  
    &lt;/TABLE&gt;  
    &lt;SCRIPT&gt;  
allCount=4  
&lt;/SCRIPT&gt;  
    &lt;TABLE align=center border=1 borderColorDark=white borderColorLight=gray  
cellPadding=1 cellSpacing=1 class=controlPanel cols=4 rules=all width="100%"  
nowrap&gt;  
      &lt;TBODY&gt;  
      &lt;TR&gt;  
        &lt;TD align=middle class=controlPanel onclick=newHospitalVisit()  
    onmouseout="this.className='controlPanel'"  
    onmouseover="this.className='controlPanelMouseOver'"&gt;&lt;FONT size=2&gt;Add a New  
          Record, click here&lt;/FONT&gt;&lt;/TD&gt;  
      &lt;/TR&gt;  
      &lt;/TBODY&gt;  
    &lt;/TABLE&gt;  
  &lt;/DIV&gt;  
&lt;/FORM&gt;  
&lt;/BODY&gt;  
&lt;/HTML&gt;