search
HomeWeb Front-endJS TutorialIn-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(&#39;tab1&#39;);            //获取第二行的&#39;张三&#39;//            alert( oTab.getElementsByTagName(&#39;tbody&#39;)[0].getElementsByTagName(&#39;tr&#39;)[1].getElementsByTagName(&#39;td&#39;)[1].innerHTML);
            //table独有的简单操作
            //上面的代码可以简写成下面的格式;//            alert( oTab.tBodies(&#39;tbody&#39;)[0].rows(&#39;tr&#39;)[1].cells(&#39;td&#39;)[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 = &#39;green&#39;;
                }
                aRow[i].onmouseout = function () {                    this.style.backgroundColor = oldColor;
                }                if (i%2){
                    aRow[i].style.background = &#39;&#39;;
                }else {
                    aRow[i].style.background = &#39;#ccc&#39;;
                }
            }
        }    </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(&#39;tab1&#39;);            var oName = document.getElementById(&#39;name&#39;);            var oAge = document.getElementById(&#39;age&#39;);            var oBtn = document.getElementById(&#39;btn1&#39;);
            oBtn.onclick = function () {                var oTr = document.createElement(&#39;tr&#39;);                var oTd = document.createElement(&#39;td&#39;);
                oTd.innerHTML = oTab.tBodies[0].rows.length+1;
                oTr.appendChild(oTd);                var oTd = document.createElement(&#39;td&#39;);
                oTd.innerHTML = oName.value;
                oTr.appendChild(oTd);                var oTd = document.createElement(&#39;td&#39;);
                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(&#39;tab1&#39;);            var oName = document.getElementById(&#39;name&#39;);            var oAge = document.getElementById(&#39;age&#39;);            var oBtn = document.getElementById(&#39;btn1&#39;);            //行数
            var vRow = oTab.tBodies[0].rows.length+1;
            oBtn.onclick = function () {                var oTr = document.createElement(&#39;tr&#39;);
                vRow++;                var oTd = document.createElement(&#39;td&#39;);
                oTd.innerHTML = vRow;
                oTr.appendChild(oTd);                var oTd = document.createElement(&#39;td&#39;);
                oTd.innerHTML = oName.value;
                oTr.appendChild(oTd);                var oTd = document.createElement(&#39;td&#39;);
                oTd.innerHTML = oAge.value;
                oTr.appendChild(oTd);                var oTd = document.createElement(&#39;td&#39;);
                oTd.innerHTML = &#39;<a href="javaScript:;">删除</a>&#39;;
                oTr.appendChild(oTd);
                oTd.getElementsByTagName(&#39;a&#39;)[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(&#39;tab1&#39;);            var oTxt = document.getElementById(&#39;name&#39;);            var oBtn = document.getElementById(&#39;btn1&#39;);
            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(&#39; &#39;);                    //先把背景颜色重置
                    oTab.tBodies[0].rows[i].style.backgroundColor = &#39;&#39;;                    for (var j=0;j<arr.length;++j){                        if (sTab.search(arr[j]) != -1){
                            oTab.tBodies[0].rows[i].style.backgroundColor = &#39;yellow&#39;;
                        }
                    }
                }
            }
        }    </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(&#39;btn1&#39;);            var oUl = document.getElementById(&#39;ul1&#39;);
            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(&#39;tab1&#39;);            var oBtn = document.getElementById(&#39;btn1&#39;);
            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!

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 Data Types : Is there any difference between Browser and NodeJs?Javascript Data Types : Is there any difference between Browser and NodeJs?May 14, 2025 am 12:15 AM

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScript Comments: A Guide to Using // and /* */JavaScript Comments: A Guide to Using // and /* */May 13, 2025 pm 03:49 PM

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

Python vs. JavaScript: A Comparative Analysis for DevelopersPython vs. JavaScript: A Comparative Analysis for DevelopersMay 09, 2025 am 12:22 AM

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.

Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

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: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

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.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

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

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

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.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

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.

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 Article

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools