JS Modify the value of TD in table (define TD's Double-click event) _javascript Tips

Source: Internet
Author: User
Customer demand is paramount.
This is the changed code, remove the "OK", "Cancel" button. Click to double-click the event, the user presses the ESC key to cancel the change.
Copy Code code as follows:

Add a double click event for each TD
function rewritable ()
{
var Tbmian=document.getelementbyid ("Tbmain");
Loops, adding a double click event for each row of columns, but the first row (THEAD) is not added to the last row (TFOOT).
for (Var i=1;i<tbmain.rows.length-1;i++)
{
for (Var j=0;j<tbmain.rows[i].cells.length;j++)
{
Tbmain.rows[i].cells[j].ondblclick=tddoubleclick;
}
}
}
Define TD's Double-click event, add a text box to it, and use the user to enter it.
function Tddoubleclick ()
{
The first thing to decide is whether the box already exists, and if it does, it returns and does not add the text box repeatedly. If it does not exist, it is added.
var Tdcag=document.getelementbyid ("Tdcag");
var tdid=null;
var txtid=null;
var curtd=window.event.srcelement;
if (tdcag!=null)//already exists, returns.
{
Return
}
Does not exist, add the
Tdid=window.event.srcelement;
Tdtxt=tdid.innertext;
Tdtxt=trim (Tdtxt);
var str= "<div id= ' tdcag ' ><input type= ' text ' onblur= ' Changetdtext (); ' id= ' txtID ' value= ' +tdtxt+ ' ' > ';
str+= "<input type= ' hidden ' id= ' tdinitvalue ' value= '" "+tdtxt+" ' > ";
str+= "</div>";
TDID.INNERHTML=STR;
Gets the focus of the text box.
document.getElementById ("txtID"). focus ();
var Ctr=document.getelementbyid ("txtID"). createTextRange ();
Ctr.collapse (FALSE);
Ctr.select ();
}
Remove the spaces before and after the string.
function Trim (str)
{
Return Str.replace (/(^\s*) | ( \s*$)/g, "");
}
Defines the event when KeyPress is pressed and, if it is the ESC key, cancels the change and reverts to the value before the change.
document.onkeypress = function esckeypress ()
{
if (event.keycode==27)
{
Canceltdchanged ();
Return
}
}
Cancel the change,
function canceltdchanged ()
{
var Tdinitvalue=document.getelementbyid ("Tdinitvalue");
var Tdtxt=tdinitvalue.value;
var Tdid=document.getelementbyid ("Tdcag"). parentnode;
Tdid.innertext=trim (Tdtxt);
}
Determine the changes,
function Changetdtext ()
{
var Txtid=document.getelementbyid ("txtID");
if (txtid==null)
{
Return
}
var Tdid=document.getelementbyid ("Tdcag"). parentnode;
Tdid.innertext=trim (Txtid.value);
Return
}

Other code, same as before. Only Thead and tfoot changes to the table are canceled.
Copy Code code as follows:

<body onload= "rewritable ();" >
<table id= "Tbmain" style= width:100%; "border=" 1 ">
<thead>
<tr>
<td>t Head,thead,thead</td>
</tr>
</thead>
<tfoot>
<tr>
<td>
Tfoot,tfoot,tfoot
</td>
</tr>
</tfoot>
<tr>
<td>11
</td>
<td>12
</td>
<td>13</td>
</tr>
<tr>
& L T;td>21
</td>
<td>22
</td>
<td>23
</td>
</tr>
<tr>
<td>31
</td>
<td>32
</td>
<td>33
</td >
</tr>
</table>
</body>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.