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

Source: Internet
Author: User
Project completed, in the test acceptance, the user suddenly raised, you can change the results of the query, easy to print. Hide engineering errors. But this time ask, compare headache, later, think or use the foreground code, so, as long as in the project JS file, add the following functions, it can be solved.
Copy Code code as follows:

/*
When the page is loaded, the click events are incremented for each TD so that you do not have to make changes to each page.
*/
function rewritable ()
{
var Tbmian=document.getelementbyid ("Tbmain");
for (Var i=0;i<tbmain.rows.length;i++)
{
for (Var j=0;j<tbmain.rows[i].cells.length;j++)
{
/*
Add Click event Properties. The SetAttribute method cannot be used here.
*/
Tbmain.rows[i].cells[j].onclick=addobjoftext;
}
}
}
/*
Click the event to update the TD content to a DIV, which loads a text for the user to enter the value of the new TD.
A OK button, a Cancel button, to save or cancel the user's input.
A hidden that saves the value of the TD before the user enters the new value, so that the user can recover when it is canceled.
*/
function Addobjoftext ()
{
var Tdcag=document.getelementbyid ("Tdcag");
if (tdcag!=null)
{
Return
}
var tdid=window.event.srcelement;
var Tdtxt=tdid.innertext;
var str= "<div id= ' tdcag ' ><input type= ' text ' id= ' txtid ' value= '" "+tdtxt+" ' > ";
str+= "<input type= ' button ' value= ' determines ' onclick= ' Changetdtext () ' >";
str+= "<input type= ' button ' value= ' Cancel ' onclick= ' canceltdchanged () ' >";
str+= "<input type= ' hidden ' id= ' tdinitvalue ' value= '" "+tdtxt+" ' > ";
str+= "</div>";
TDID.INNERHTML=STR;
}
/*
Cancels the change, assigns the hidden value to the TD
*/
function canceltdchanged ()
{
var Tdinitvalue=document.getelementbyid ("Tdinitvalue");
var Tdtxt=tdinitvalue.value;
var Tdid=document.getelementbyid ("Tdcag"). parentnode;
Tdid.innertext=tdtxt;
}
/*
Save user changes and assign text values to TD
*/
function Changetdtext ()
{
var Txtid=document.getelementbyid ("txtID");
var Tdid=document.getelementbyid ("Tdcag"). parentnode;
Tdid.innertext=txtid.value;
}

In this way, in the <body> of the page, add the OnLoad event, whose value is: rewritable (), as follows:
Copy Code code as follows:

<body onload= "rewritable ();" >
<table id= "Tbmain" style= "width:100%;" border= "1" >
<tr>
<td>11
</td>
<td>12
</td>
<td>13
</td>
</tr>
<tr>
<td>21
</td>
<td>22
</td>
<td>23
</td>
</tr>
<tr>
<td>31
</td>
<td>32
</td>
<td>33
</td>
</tr>
</table>
</body>

Thus, in each TD, a click event is added.

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.