search
HomeWeb Front-endJS TutorialUse jQuery to implement editable tables_jquery

Today we learned the example of using jQuery to implement an editable table. The requirements for this example are as follows: click on a cell in the foreground table to modify its content, press Enter to save the modified content, and esc to undo the saved content. Principle: When you click a client table cell, add a text box in the cell, assign the original content in the cell to the text box, and then further modify the text box content. After modification, reassign the text box content to cell.

Source code:

Frontend code:

Copy code The code is as follows:


BR>


jq2—editable table



--%>








tr>




> ;






>


;



You can edit table items by clicking the mouse
Student number Name
000001 Zhang San
000002 李思000003 王五
Zhao Liu







css code:



Copy code
The code is as follows: body { } table {
border:1px solid #000000;
border-collapse:collapse ;/*Cell border merging*/
width:400px;
}
table td {
border:1px solid #000000;
width:50%;
}
table th {
border:1px solid #000000;
width:50%;
}
tbody th {
background-color:#426fae;
}


jquery code



Copy code
The code is as follows:

$(function () {
//Find all even rows in the table except the first tr
//Use even in order to return all tr ​​elements through tbody tr
$( "tbody tr:even").css("background-color", "#ece9d8");
//Find all student number cells
var numId = $("tbody td:even");

//Register mouse click event for the cell
numId.click(function () {
//Find the td corresponding to the current mouse click, this corresponds to the td that responded to the click
var tdObj = $(this);
//Determine whether there is a text box in td
if (tdObj.children("input").length>0) {
return false;
}
//Get the content in the table
var text = tdObj.html();
//Clear the content in td
tdObj.html("");
//Create Text box
//Remove the border of the text box
//Set the font size in the text box to be the same as the text in the table
//Set the background color of the text box to be the same as the background color of the table
. //The width of the text box is the same as the width of td
//Put the value in td into the text box
//Insert the text box into td
var inputObj = $("").css("border-width", "0").css("font-size", tdObj.css("font-size")).css("background-color ", tdObj.css("background-color")).width(tdObj.width()).val(text).appendTo(tdObj);
//After the text box is inserted, it gets focus first and then selects it
inputObj.trigger("focus").trigger("select")
//The click event cannot be triggered after the text box is inserted
inputObj.click(function () {
return false;
});
//Process the operation of the enter and esc keys on the text box
inputObj.keyup(function (event) {
//Get the key value of the currently pressed keyboard
var keycode = event.which;
//Handle carriage return
if (keycode==13) {
//Get the content in the current text box
var inputtext = $(this).val ();
//Modify the content in td to the content of the text box
tdObj.html(inputtext);
}
//Process the content of esc
if (keycode== 27) {
//Restore the content in td to the original content
tdObj.html(text);
}
});
});
});

Summary: Knowledge points that can be obtained through learning from this example:

1. HTML

1.table can contain thead and tbody

2. The content of the table header can be placed in th

3. table{} This writing method is called a label selector and can have an impact on the entire table.

4.table td{} represents all tds contained in the table.

2. In terms of jquery

$() can put 4 different parameters in the brackets

1. Put the parameter directly into function, indicating that the page is loaded: For example, the above example Line 1 in the jquery code $(function(){})

2. The parameter can be a css class selector and is packaged into a jquery object. For example: Line 4 of the jquery code in the above example $("tbody tr:even")

3. If the parameter is HTML text, you can create a dom node and package it into a jquery object. For example: Line 27 of the jquery code in the above example $("")

4. The parameter can be a dom object. This method is equivalent to replacing the dom object. into a jquery object. Line 11 of the jquery code in the above example var tdObj = $(this)

The jquery object in this example

1. Add css attributes after the jquery object to set the css attributes of the node. For example, line 4 in the jquery code in the above example $("tbody tr:even").css("background-color", "#ece9d8");

2.jquery object content contains selection The DOM node corresponding to the device is saved in an array.

3. Adding the html method after the jquery object can set or get the html content of the node. For example, line 17 of the jquery code in the above example var text = tdObj.html()

4. Adding the val method after the jquery object can get or set the value of the node. For example, in the above example, line 41 of the jquery code var inputtext = $(this).val()

5. Adding the width method after the jquery object can set or get the width of a node. For example, line 27 of the jquery code in the above example is tdObj.width()

6. Adding the appendTo method after the jquery object can append a node to all the child nodes of another node. For example, in the above example, line 27 appendTo(tdObj) in the jquery code

7. Adding the trigger method after the jquery object can trigger a js event to occur. For example, in the above example, line 29 of the jquery code inputObj.trigger("focus").trigger("select")

8. Adding the children method after the jquery object can obtain the child nodes of a certain node, which can be formulated Parameters to limit the content of child nodes. For example, line 13 of the jquery code in the above example tdObj.children("input").length

9. If the jquery object returned by the selector contains multiple dom nodes, register a similar click event on this object , all DOM nodes will be used for this event. For example, line 9 numId.click in the jquery code in the above example;
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
JavaScript in Action: Real-World Examples and ProjectsJavaScript in Action: Real-World Examples and ProjectsApr 19, 2025 am 12:13 AM

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

JavaScript and the Web: Core Functionality and Use CasesJavaScript and the Web: Core Functionality and Use CasesApr 18, 2025 am 12:19 AM

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding the JavaScript Engine: Implementation DetailsUnderstanding the JavaScript Engine: Implementation DetailsApr 17, 2025 am 12:05 AM

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python vs. JavaScript: The Learning Curve and Ease of UsePython vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python vs. JavaScript: Community, Libraries, and ResourcesPython vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

From C/C   to JavaScript: How It All WorksFrom C/C to JavaScript: How It All WorksApr 14, 2025 am 12:05 AM

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

JavaScript Engines: Comparing ImplementationsJavaScript Engines: Comparing ImplementationsApr 13, 2025 am 12:05 AM

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Beyond the Browser: JavaScript in the Real WorldBeyond the Browser: JavaScript in the Real WorldApr 12, 2025 am 12:06 AM

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.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!