


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's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Python is more suitable for data science and machine learning, while JavaScript is more suitable for front-end and full-stack development. 1. Python is known for its concise syntax and rich library ecosystem, and is suitable for data analysis and web development. 2. JavaScript is the core of front-end development. Node.js supports server-side programming and is suitable for full-stack development.

JavaScript does not require installation because it is already built into modern browsers. You just need a text editor and a browser to get started. 1) In the browser environment, run it by embedding the HTML file through tags. 2) In the Node.js environment, after downloading and installing Node.js, run the JavaScript file through the command line.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Atom editor mac version download
The most popular open source editor

Dreamweaver CS6
Visual web 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.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.