Create editable table effects based on JQuery _ jquery

Source: Internet
Author: User
This article mainly introduces how to create editable table effects based on JQuery and provides two solutions. Can you tell me which solution is more reasonable? Recently, a project was created. project requirements: click a table and you can directly edit the project. Press ENTER or click another place on the page to edit the project. Press Esc to cancel editing.

The two partners provided the solution for the second phase. Let's take a look at which one is more appropriate?

First, you can click a table to edit the table.

The Code is as follows:


// Equivalent to adding an onload event to the body tag of the page
$ (Function (){
// Find all td nodes
Var tds = $ ("td ");
// Add a click event to all td
Tds. click (function (){
// Obtain the object of the current click
Var td = $ (this );
// Extract the text content of the current td and save it
Var oldText = td. text ();
// Create a text box and set the value of the text box to the saved value.
Var input = $ ("");
// Set the content of the current td object to input
Td.html (input );
// Set the Click Event of the text box to invalid
Input. click (function (){
Return false;
});
// Set the text box style
Input.css ("border-width", "0 ");
Input.css ("font-size", "16px ");
Input.css ("text-align", "center ");
// Set the text box width to equal to the width of td
Input. width (td. width ());
// Trigger the all-selected event when the text box gets the focus
Input. trigger ("focus"). trigger ("select ");
// Changes to text when the text box loses focus
Input. blur (function (){
Var input_blur = $ (this );
// Save the content of the current text box
Var newText = input_blur.val ();
Td.html (newText );
});
// Responds to Keyboard Events
Input. keyup (function (event ){
// Obtain the key value
Var keyEvent = event | window. event;
Var key = keyEvent. keyCode;
// Obtain the current object
Var input_blur = $ (this );
Switch (key)
{
Case 13: // press enter to save the content of the current text box
Var newText = input_blur.val ();
Td.html (newText );
Break;
Case 27: // Press esc to cancel the modification and convert the text box to text.
Td.html (oldText );
Break;
}
});
});
});

Second, you can click a table to edit the table.

The Code is as follows:


$ (Document). ready (function (){
Var tds = $ ("td ");
Tds. click (tdClick );
});
Function tdClick (){
Var tdnode = $ (this );
Var tdtext = tdnode. text ();
Tdnode.html ("");
Var input = $ ("");
Input. val (tdtext); // input. attr ("value", tdtext );
Input. keyup (function (event ){
Var myEvent = event | window. event;
Var keyCode = myEvent. keyCode;
If (keyCode = 13 ){
Var inputnode = $ (this );
Var inputtext = inputnode. val ();
Var td = inputnode. parent ();
Td.html (inputtext );
Td. click (tdClick );
}
If (keyCode = 27) {// determines whether to press the ESC key
Optional (this).parent().html (tdtext );
$ (This). parent (). click (tdClick );
}
});
Tdnode. append (input );
Tdnode. children ("input"). trigger ("select ");
// The input box loses focus and the method to be executed
Input. blur (function (){
Tdnode.html ($ (this). val ());
Tdnode. click (tdClick );
});
Tdnode. unbind ("click ");
}


In comparison, I prefer the second type. What are your comments? Please leave a message to me.

Related Article

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.