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>
<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 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></head><body><div id="div1"> <div id="form"> <input id="btn1" type="button" value="排序"> </div> <table id="tab1"> <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></body></html>
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!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics

Deleted something important from your home screen and trying to get it back? You can put app icons back on the screen in a variety of ways. We have discussed all the methods you can follow and put the app icon back on the home screen. How to Undo Remove from Home Screen in iPhone As we mentioned before, there are several ways to restore this change on iPhone. Method 1 – Replace App Icon in App Library You can place an app icon on your home screen directly from the App Library. Step 1 – Swipe sideways to find all apps in the app library. Step 2 – Find the app icon you deleted earlier. Step 3 – Simply drag the app icon from the main library to the correct location on the home screen. This is the application diagram

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

The role and practical application of arrow symbols in PHP In PHP, the arrow symbol (->) is usually used to access the properties and methods of objects. Objects are one of the basic concepts of object-oriented programming (OOP) in PHP. In actual development, arrow symbols play an important role in operating objects. This article will introduce the role and practical application of arrow symbols, and provide specific code examples to help readers better understand. 1. The role of the arrow symbol to access the properties of an object. The arrow symbol can be used to access the properties of an object. When we instantiate a pair

The Linuxtee command is a very useful command line tool that can write output to a file or send output to another command without affecting existing output. In this article, we will explore in depth the various application scenarios of the Linuxtee command, from entry to proficiency. 1. Basic usage First, let’s take a look at the basic usage of the tee command. The syntax of tee command is as follows: tee[OPTION]...[FILE]...This command will read data from standard input and save the data to

The relationship between js and vue: 1. JS as the cornerstone of Web development; 2. The rise of Vue.js as a front-end framework; 3. The complementary relationship between JS and Vue; 4. The practical application of JS and Vue.

The Go language is an open source programming language developed by Google and first released in 2007. It is designed to be a simple, easy-to-learn, efficient, and highly concurrency language, and is favored by more and more developers. This article will explore the advantages of Go language, introduce some application scenarios suitable for Go language, and give specific code examples. Advantages: Strong concurrency: Go language has built-in support for lightweight threads-goroutine, which can easily implement concurrent programming. Goroutin can be started by using the go keyword

The wide application of Linux in the field of cloud computing With the continuous development and popularization of cloud computing technology, Linux, as an open source operating system, plays an important role in the field of cloud computing. Due to its stability, security and flexibility, Linux systems are widely used in various cloud computing platforms and services, providing a solid foundation for the development of cloud computing technology. This article will introduce the wide range of applications of Linux in the field of cloud computing and give specific code examples. 1. Application virtualization technology of Linux in cloud computing platform Virtualization technology

MySQL timestamp is a very important data type, which can store date, time or date plus time. In the actual development process, rational use of timestamps can improve the efficiency of database operations and facilitate time-related queries and calculations. This article will discuss the functions, features, and application scenarios of MySQL timestamps, and explain them with specific code examples. 1. Functions and characteristics of MySQL timestamps There are two types of timestamps in MySQL, one is TIMESTAMP
