This time I will bring you jQuery to create editable tables (with code). What are the precautions for jQuery to create editable tables? Here are practical cases, let’s take a look.
nbsp;html <meta> <title>jq2—可以编辑的表格</title> <link> <script></script> <script></script> --%>
css code:
body { } table { border:1px solid #000000; border-collapse:collapse;/*单元格边框合并*/ 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
$(function () { //找到表格中除了第一个tr以外的所有偶数行 //使用even为了通过tbody tr返回所有tr元素 $("tbody tr:even").css("background-color", "#ece9d8"); //找到所有的学号单元格 var numId = $("tbody td:even"); //给单元格注册鼠标点击事件 numId.click(function () { //找到对应当前鼠标点击的td,this对应的就是响应了click的那个td var tdObj = $(this); //判断td中是否有文本框 if (tdObj.children("input").length>0) { return false; } //获取表格中的内容 var text = tdObj.html(); //清空td中的内容 tdObj.html(""); //创建文本框 //去掉文本框的边框 //设置文本框中字体与表格中的文字大小相同。 //设置文本框的背景颜色与表格的背景颜色一样 //是文本框的宽度和td的宽度相同 //并将td中值放入文本框中 //将文本框插入到td中 var inputObj = $("<input>").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); //文本框插入后先获得焦点、后选中 inputObj.trigger("focus").trigger("select") //文本框插入后不能被触发单击事件 inputObj.click(function () { return false; }); //处理文本框上回车和esc按键的操作 inputObj.keyup(function (event) { //获取当前按下键盘的键值 var keycode = event.which; //处理回车的情况 if (keycode==13) { //获取当前文本框中的内容 var inputtext = $(this).val(); //将td中的内容修改为文本框的内容 tdObj.html(inputtext); } //处理esc的内容 if (keycode==27) { //将td中的内容还原成原来的内容 tdObj.html(text); } }); }); });
Summary: Knowledge points that can be obtained through the study of this example:
1. HTML aspect
1. The 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 tag selector , which can select the entire table Make an impact.
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, which means that the page is loaded: For example, line 1 in the jquery code in the above example$ (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 with a jquery object. . Line 11 of the jquery code in the above example var tdObj = $(this)
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. The jquery object content contains the selector corresponding DOM node, saved as 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. Add the val method after the jquery object to 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 tdObj.width() in the jquery code in the above example
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 node, and parameters can be set to limit The content of the child node. 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, in the above example, line 9 numId.click in the jquery code;
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!
Recommended reading:
Detailed explanation of the difference between JSON.parse() and JSON.stringify() and how to use it
JS implements JSON .stringify steps detailed explanation
The above is the detailed content of jQuery creates editable tables (with code). For more information, please follow other related articles on the PHP Chinese website!

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 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.

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.

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.

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.


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

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver CS6
Visual web development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

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

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.