


jQuery implements cross-browser editable tables step by step, supporting IE, Firefox, Safari, Chrome, Opera_jquery
To implement the editable table function, we need to solve the following problems:
1. Clarify which columns in the table the data to be modified are (how to find these cells);
2. How to make the cells editable Edited;
3. How to handle some key events of cells;
4. Solve cross-browser issues.
We can solve the above problems step by step through jQuery.
1. Draw a table
First we draw a table. " -//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Click on the table to edit | |
---|---|
Student number | Name | 000001 | Zhang San |
|
李思 |
000003 | 王五 |
000004 | Zhao Liu td> |
After drawing the table, it will be displayed as shown below:
editTable01.jpg
Obviously it doesn’t look like a table, it has no borders and is ugly. So let's set some styles for this table first.
Code2:
Copy code
The code is as follows:

body{}{
font-size: 14px;
}
}
td{}{
width: 50%;
border: 1px solid #C1DAD7;
}
The effect is much better now:
editTable02.jpg
But there are still overlapping borders between cells. You only need to add such an attribute to the label selector table to remove duplicate borders:
border-collapse: collapse;
Copy code
The code is as follows:

table{}{
color: #4F6B72;
border: 1px solid #C1DAD7;
After drawing the table, we select the numbered column in the table as an editable column. To make the cells in this column editable, you need to insert text boxes in these columns. We insert text boxes through the onclick event of the cells in this column.
Code3:
Copy code
The code is as follows:

$(document).ready(function(){
//Find all the cells in the column of student number
//Because the cells in the column of student number are in all TDs is an even number (0,2,4,6), so you can filter to the even-numbered cells in td through even
var numTd = $("tbody td:even");
//Click these td, create a text box
numTd.click(function(){
//Create a text box object
var inputobj = $("");
//Get the currently clicked cell object
var tdobj = $(this);
//Remove the border of the text box
inputobj.css("border","0");
//Make the width of the text box and the cell consistent
inputobj.width(tdobj.width());
//Make the font size of the text box the same as the font size of the cell
inputobj.css( "font-size",tdobj.css("font-size"));
//Make the text box and cell font consistent
inputobj.css("font-family",tdobj.css( "font-family"));
//Make the background of the text box and cell consistent
inputobj.css("background-color",tdobj.css("background-color"));
//appendTo method adds the text box to td
inputobj.appendTo(tdobj);
});
});
Now the text box has been inserted into The cell is in. Since we want to edit the text box, the text box should have a value. The value of the text box comes from the data in the cell, and we need to clear the original data in the cell.
Code4:
$(document).ready( function(){
//Find all cells in the student number column
//Because the position of the cells in the student number column in all td is an even number (0, 2, 4, 6), so Through even, you can filter to even-numbered cells in td
var numTd = $("tbody td:even");
//When you click these td, create a text box
numTd.click( function(){
//Create a text box object
var inputobj = $("");
//Get the currently clicked cell object
var tdobj = $(this);
//Get the text in the cell
var text = tdobj.html();
//Clear the text in the cell
tdobj.html("" );
//Remove the border of the text box
inputobj.css("border","0");
//Make the width of the text box and cell consistent
inputobj.width( tdobj.width());
//Make the font size of the text box the same as the font size of the cell
inputobj.css("font-size",tdobj.css("font-size"));
//Make the fonts of the text box and the cell consistent
inputobj.css("font-family",tdobj.css("font-family"));
//Make the text box and the cell the same The background remains consistent
inputobj.css("background-color",tdobj.css("background-color"));
inputobj.css("color","#C75F3E");
/ /Assign a value to the text box
inputobj.val(text);
//The appendTo method adds the text box to td
inputobj.appendTo(tdobj);
});
}) ;
But the above code looks very cumbersome. jQuery has a very good advantage, that is, its code concatenation. The above code can be simplified by concatenating:
Code5:
$(document).ready(function(){
//Find all cells in the student number column
//Because the position of the cells in the student number column in all td is an even number (0 ,2,4,6), so you can filter to even-numbered cells in td through even
var numTd = $("tbody td:even");
//When you click these td, create Text box
numTd.click(function(){
//Create text box object
var inputobj = $("");
//Get The currently clicked cell object
var tdobj = $(this);
//Get the text in the cell
var text = tdobj.html();
//Clear the text of the cell
tdobj.html("");
inputobj.css("border","0")
.css("font-size",tdobj.css("font-size"))
.css("font-family",tdobj.css("font-family"))
.css("background-color",tdobj.css("background-color"))
.css ("color","#C75F3E")
.width(tdobj.width())
.val(text)
.appendTo(tdobj);
});
}) ;
Now the text box has been successfully inserted into the table and the cells can be edited.
editTable04.jpg

bug, when you click the same cell again, the following effect will appear:
editTable05.jpg
What is the cause of the above bug? Because after a cell is inserted into the text box, the text box belongs to the cell. When we click on the text box, the click event of the cell will also be triggered.
We need to prevent the click behavior of the text box (prevent event bubbling).
Code6:
return false;
});
Code7:
//Find all cells in the student number column
//Because the position of the cells in the student number column in all td is an even number (0, 2, 4, 6), so Through even, you can filter to even-numbered cells in td
var numTd = $("tbody td:even");
//When you click these td, create a text box
numTd.click( function(){
//Create a text box object
var inputobj = $("");
//Get the currently clicked cell object
var tdobj = $(this);
//Get the text in the cell
var text = tdobj.html();
//If there is a text box in the current cell, jump out of the method directly
//Note: Be sure to make a judgment before inserting the text box
if(tdobj.children("input").length>0){
return false;
}
//Clear Cell text
tdobj.html("");
inputobj.css("border","0")
.css("font-size",tdobj.css("font-size "))
.css("font-family",tdobj.css("font-family"))
.css("background-color",tdobj.css("background-color"))
.css("color","#C75F3E")
.width(tdobj.width())
.val(text)
.appendTo(tdobj);
inputobj.get( 0).select();
//Prevent the click event of the text box
inputobj.click(function(){
return false;
});
});
});
Code 8:
Code9:
Now that the above problems have been solved, let's add some key events to the text box. We know that the keyCode for obtaining keystrokes is different in different browsers, but jQuery helps us solve this problem.
You only need to add the event parameter to the function of the event, and then in the method body, you can obtain the keyCode through the which attribute of the event object. The event.which attribute assimilates the methods of obtaining keyCode in different browsers.
After obtaining the keyCode, I mainly do two key events: ESC key (key value: 27) and Enter key (key value: 13).
Code10:
//Handle the operations of the enter and esc keys on the text box
//The function of an event method in jQuery can define an event parameter, and jQuery will block the differences between browsers and pass it to us Available event objects
inputobj.keyup(function(event){
//Get the key value of the current key
//There is a which attribute on the jQuery event object to get the key value of the keyboard key
var keycode = event.which;
//Handle carriage return
if(keycode==13){
//Get the content of the current text box
var inputtext = $(this ).val();
//Modify the content of td to the content in the text box
tdobj.html(inputtext);
}
//Handle the situation of esc
if( keycode == 27){
//Restore the content in td to text
tdobj.html(text);
}
});
The following is Complete js code:
Code11:
$( document).ready(function(){
//Find all the cells in the column of student number
//Because the position of the cells in the column of student number in all td is an even number (0,2,4 ,6), so you can filter to the even-numbered cells in td through even
var numTd = $("tbody td:even");
//Create a text box when you click these tds
numTd.click(function(){
//Create a text box object
var inputobj = $("");
//Get the currently clicked unit Cell object
var tdobj = $(this);
//Get the text in the cell
var text = tdobj.html();
//If there is a text box in the current cell, Just jump out of the method
//Note: Be sure to make a judgment before inserting the text box
if(tdobj.children("input").length>0){
return false;
}
//Clear the text of the cell
tdobj.html("");
inputobj.css("border","0")
.css("font-size",tdobj.css ("font-size"))
.css("font-family",tdobj.css("font-family"))
.css("background-color",tdobj.css("background- color"))
.css("color","#C75F3E")
.width(tdobj.width())
.val(text)
.appendTo(tdobj);
inputobj.get(0).select();
//Prevent the click event of the text box
inputobj.click(function(){
return false;
});
/ /Handle the operation of the enter and esc keys on the text box
//The function of an event method in jQuery can define an event parameter. jQuery will mask the differences between browsers and pass us a usable event object
inputobj.keyup(function(event){
//Get the key value of the current key
//There is a which attribute on the jQuery event object to get the key value of the keyboard key
var keycode = event. which;
//Handle carriage return
if(keycode==13){
//Get the content of the current text box
var inputtext = $(this).val();
//Modify the content of td to the content in the text box
tdobj.html(inputtext);
}
//Handle the situation of esc
if(keycode == 27){
//Restore the content in td to text
tdobj.html(text);
}
});
});
});
Related document package download

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Zend Studio 13.0.1
Powerful PHP integrated development environment

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
