In-depth analysis of advanced applications of JavaScript's DOM
This time I will bring you an in-depth advanced application of DOM in JavaScript. What are the precautions for advanced DOM applications using JavaScript? Here are practical cases. Let’s take a look. .
Changing colors on alternate rows
<html lang="en"><head> <meta charset="UTF-8"> <title>04-表格的应用</title> <style> table{ margin: 100px auto; width: 300px; text-align: center; background-color: black; } table tr { background-color: white; } </style> <script> window.onload = function () { var oTab = document.getElementById('tab1'); //获取第二行的'张三'// alert( oTab.getElementsByTagName('tbody')[0].getElementsByTagName('tr')[1].getElementsByTagName('td')[1].innerHTML); //table独有的简单操作 //上面的代码可以简写成下面的格式;// alert( oTab.tBodies('tbody')[0].rows('tr')[1].cells('td')[1].innerHTML); /** *90年代,div+css没人用,人们用的几乎全是table,于是乎,就有了table的便捷操作. * tBodies(一个table里可以有多个tbody),tHead,tFoot,rows,cells等便捷操作 * */ //隔行变色 var aRow = oTab.tBodies[0].rows;// alert(aRow.length); //记录一下颜色 var oldColor = null; for(var i=0;i<aRow.length;i++){ aRow[i].onmouseover = function () { //先记录一下之前的颜色 oldColor = this.style.backgroundColor; this.style.background = 'green'; } aRow[i].onmouseout = function () { this.style.backgroundColor = oldColor; } if (i%2){ aRow[i].style.background = ''; }else { aRow[i].style.background = '#ccc'; } } } </script></head><body><table id="tab1"> <thead> <td>ID</td> <td>姓名</td> <td>年龄</td> </thead> <tbody> <tr> <td>1</td> <td>Blue</td> <td>27</td> </tr> <tr> <td>2</td> <td>张三</td> <td>32</td> </tr> <tr> <td>3</td> <td>李四</td> <td>17</td> </tr> <tr> <td>4</td> <td>王五</td> <td>28</td> </tr> <tr> <td>5</td> <td>张伟</td> <td>35</td> </tr> </tbody></table></body></html>
2. Adding tables
Dynamic adding of tables
<html lang="en"><head> <meta charset="UTF-8"> <title>04-表格的添加 删除</title> <style> #div1{ text-align: center; } #div1 #form{ margin: 100px 0 10px; } #div1 table{ margin: 0px auto; width: 300px; text-align: center; background-color: black; } table tr { background-color: white; } </style> <script> window.onload = function () { var oTab = document.getElementById('tab1'); var oName = document.getElementById('name'); var oAge = document.getElementById('age'); var oBtn = document.getElementById('btn1'); oBtn.onclick = function () { var oTr = document.createElement('tr'); var oTd = document.createElement('td'); oTd.innerHTML = oTab.tBodies[0].rows.length+1; oTr.appendChild(oTd); var oTd = document.createElement('td'); oTd.innerHTML = oName.value; oTr.appendChild(oTd); var oTd = document.createElement('td'); oTd.innerHTML = oAge.value; oTr.appendChild(oTd); //注意:一定要添加到第0个tBodies上 oTab.tBodies[0].appendChild(oTr); } } </script></head><body><div id="div1"> <div id="form"> 姓名:<input id="name" type="text"> 年龄:<input id="age" type="text"> <input id="btn1" type="button" value="添加"> </div> <table id="tab1"> <thead> <td>ID</td> <td>姓名</td> <td>年龄</td> </thead> <tbody> <tr> <td>1</td> <td>Blue</td> <td>27</td> </tr> <tr> <td>2</td> <td>张三</td> <td>32</td> </tr> <tr> <td>3</td> <td>李四</td> <td>17</td> </tr> <tr> <td>4</td> <td>王五</td> <td>28</td> </tr> <tr> <td>5</td> <td>张伟</td> <td>35</td> </tr> </tbody> </table></div></body></html>
3.Deleting tables
<html lang="en"><head> <meta charset="UTF-8"> <title>05-表格的添加 删除</title> <style> #div1{ text-align: center; } #div1 #form{ margin: 100px 0 10px; } #div1 table{ margin: 0px auto; width: 300px; text-align: center; background-color: black; } table tr { background-color: white; } </style> <script> window.onload = function () { var oTab = document.getElementById('tab1'); var oName = document.getElementById('name'); var oAge = document.getElementById('age'); var oBtn = document.getElementById('btn1'); //行数 var vRow = oTab.tBodies[0].rows.length+1; oBtn.onclick = function () { var oTr = document.createElement('tr'); vRow++; var oTd = document.createElement('td'); oTd.innerHTML = vRow; oTr.appendChild(oTd); var oTd = document.createElement('td'); oTd.innerHTML = oName.value; oTr.appendChild(oTd); var oTd = document.createElement('td'); oTd.innerHTML = oAge.value; oTr.appendChild(oTd); var oTd = document.createElement('td'); oTd.innerHTML = '<a href="javaScript:;">删除</a>'; oTr.appendChild(oTd); oTd.getElementsByTagName('a')[0].onclick = function () { //注意:一定要从第0个tBodies上删除. //this.parentNode 指的是 td //this.parentNode.parentNode 指的是 tr oTab.tBodies[0].removeChild(this.parentNode.parentNode); }; //注意:一定要添加到第0个tBodies上 oTab.tBodies[0].appendChild(oTr); } } </script></head><body><div id="div1"> <div id="form"> 姓名:<input id="name" type="text"> 年龄:<input id="age" type="text"> <input id="btn1" type="button" value="添加"> </div> <table id="tab1"> <thead> <td>ID</td> <td>姓名</td> <td>年龄</td> <td>操作</td> </thead> <tbody> <tr> <td>1</td> <td>Blue</td> <td>27</td> <td></td> </tr> <tr> <td>2</td> <td>张三</td> <td>32</td> <td></td> </tr> <tr> <td>3</td> <td>李四</td> <td>17</td> <td></td> </tr> <tr> <td>4</td> <td>王五</td> <td>28</td> <td></td> </tr> <tr> <td>5</td> <td>张伟</td> <td>35</td> <td></td> </tr> </tbody> </table></div></body></html>
4 .Table search
Ignore case, fuzzy search, multi-keyword search
toLowerCase() Convert the string to all lowercase;
Fuzzy search search When found, return the position ;Not found, returns -1;
<html lang="en"><head> <meta charset="UTF-8"> <title>06-表格的搜索</title> <style> #div1{ text-align: center; } #div1 #form{ margin: 100px 0 10px; } #div1 table{ margin: 0px auto; width: 300px; text-align: center; background-color: black; } table tr { background-color: white; } </style> <script> window.onload = function () { var oTab = document.getElementById('tab1'); var oTxt = document.getElementById('name'); var oBtn = document.getElementById('btn1'); oBtn.onclick = function () { for (var i=0;i<oTab.tBodies[0].rows.length;i++){ //忽略大小写 //toLowerCase() 把字符串转成全小写的形式; //把if里面两边都转成全小写的形式; var sTab = oTab.tBodies[0].rows[i].cells[1].innerHTML.toLowerCase(); var sTxt = oTxt.value.toLowerCase(); //模糊搜索 search 当找到的时候,返回位置;找不到,返回-1 //search() //多关键字搜索 //假设多个关键字之间用 空格 隔开的,以后可以使用正则表达式 var arr = sTxt.split(' '); //先把背景颜色重置 oTab.tBodies[0].rows[i].style.backgroundColor = ''; for (var j=0;j<arr.length;++j){ if (sTab.search(arr[j]) != -1){ oTab.tBodies[0].rows[i].style.backgroundColor = 'yellow'; } } } } } </script></head><body><div id="div1"> <div id="form"> 姓名:<input id="name" type="text"> <input id="btn1" type="button" value="搜索"> </div> <table id="tab1"> <thead> <td>ID</td> <td>姓名</td> <td>年龄</td> <td>操作</td> </thead> <tbody> <tr> <td>1</td> <td>Blue</td> <td>27</td> <td></td> </tr> <tr> <td>2</td> <td>张三</td> <td>32</td> <td></td> </tr> <tr> <td>3</td> <td>李四</td> <td>17</td> <td></td> </tr> <tr> <td>4</td> <td>王五</td> <td>28</td> <td></td> </tr> <tr> <td>5</td> <td>张伟</td> <td>35</td> <td></td> </tr> </tbody> </table></div></body></html>
5. Sorting
<html lang="en"><head> <meta charset="UTF-8"> <title>08-排序</title> <script> window.onload = function () { var btn = document.getElementById('btn1'); var oUl = document.getElementById('ul1'); btn.onclick = function () { //先初始化数组 var arr = []; for (var i=0;i<oUl.children.length;i++){ arr.push(oUl.children[i]); //排序 arr.sort(function (li1,li2) { //最好不要依赖隐式类型转换,提前给强转下 var n1 = parseInt(li1.innerHTML); var n2 = parseInt(li2.innerHTML); return n1-n2; }) } //再重新添加 for(var i=0;i<arr.length;i++){ oUl.appendChild(arr[i]); } } } </script></head><body><input id="btn1" type="button" value="排序"><ul id="ul1"> <li>34</li> <li>25</li> <li>9</li> <li>88</li> <li>54</li></ul></body></html>
<meta> <title>06-表格的排序</title> <style> #div1{ text-align: center; } #div1 #form{ margin: 100px 0 10px; } #div1 table{ margin: 0px auto; width: 300px; text-align: center; background-color: black; } table tr { background-color: white; } </style> <script> window.onload = function () { var oTab = document.getElementById('tab1'); var oBtn = document.getElementById('btn1'); oBtn.onclick = function () { //aTr是个集合,他没有sort()这个方法 var aTr = oTab.tBodies[0].rows; //把tr放到一个数组里面 var arr = []; for(var i=0;i<aTr.length;i++){ arr.push(aTr[i]); } //排序 arr.sort(function (tr1, tr2) { var n1 = parseInt(tr1.cells[0].innerHTML); var n2 = parseInt(tr2.cells[0].innerHTML); return n1-n2; }) //添加 for (var i=0;i<arr.length;i++){ oTab.tBodies[0].appendChild(arr[i]); } } } </script><div> <div> <input> </div> <table> <thead> <td>ID</td> <td>姓名</td> <td>年龄</td> <td>操作</td> </thead> <tbody> <tr> <td>2</td> <td>张三</td> <td>32</td> <td></td> </tr> <tr> <td>4</td> <td>王五</td> <td>28</td> <td></td> </tr> <tr> <td>5</td> <td>张伟</td> <td>35</td> <td></td> </tr> <tr> <td>1</td> <td>Blue</td> <td>27</td> <td></td> </tr> <tr> <td>3</td> <td>李四</td> <td>17</td> <td></td> </tr> </tbody> </table> </div>
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:
In-depth basic application of JavaScript
##8 basic knowledge that must be paid attention to in JS
The above is the detailed content of In-depth analysis of advanced applications of JavaScript's DOM. For more information, please follow other related articles on the PHP Chinese website!

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

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.

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


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

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.

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Chinese version
Chinese version, very easy to use

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

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