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!

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

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


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

Atom editor mac version download
The most popular open source editor

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Chinese version
Chinese version, very easy to use

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
