Example of how to dynamically modify table and merge cells using JS
This article mainly introduces the method of dynamically modifying table and merging cells in JS. It analyzes the specific operation skills of JS dynamically traversing and modifying table cells in the form of a complete example. Friends in need can refer to the following
The example of this article describes the method of dynamically modifying the table and merging cells using JS. Share it with everyone for your reference, the details are as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>table</title> <script language="javascript" type="text/javascript"> function maketotal(){ var pageCount=4; var currenttype; var indexstr=""; var tabObj = document.getElementById("printtable"); //行数 var rowCount = tabObj.rows.length; //列数 var cellCount = tabObj.rows[0].cells.length; for(var q=1;q<rowCount;q++){ choosetype=tabObj.rows[q].cells[0].innerHTML; if(q==1){ currenttype=choosetype; } if(currenttype!=choosetype){ indexstr=q+","+indexstr; } } alert(indexstr.split(",").length); for(var p=0;p<indexstr.split(",").length;p++){ var temp=indexstr.split(",")[p]; if(temp!=""){ var newRow = tabObj.insertRow(temp); for(var g=0;g<cellCount;g++) { var littleCount=0; for(var h=1;h<rowCount;h++){ if(tabObj.rows[h].cells[g]){ if(g>1){ littleCount=littleCount+parseInt(tabObj.rows[h].cells[g].innerHTML); } } } if(g>1&&g!=6){ newRow.insertCell(g).innerHTML=littleCount; }else if(g==6){ newRow.insertCell(g).innerHTML=""; }else{ newRow.insertCell(g).innerHTML="小计"; } } } } var newRow = tabObj.insertRow(0); for(var g=0;g<cellCount;g++) { if(g==0){ //newRow.insertCell(g).innerHTML="头部"; }else{ //newRow.insertCell(g).innerHTML="头部"; } //alert(tabObj.rows[1].cells[g]); newRow.insertCell(g).innerHTML="头部"; //tabObj.rows[0].cells[g].colsSpan =2; //tabObj.rows[0].cells[g].rowsSpan =2; tabObj.rows[1].cells[g].style.background="#CCCCCC"; tabObj.rows[0].cells[g].style.background="#CCCCCC"; //tabObj.rows[0].cells[g].rowSpan=2 } tabObj.rows[0].cells[2].colSpan =2; tabObj.rows[0].cells[2].innerHTML="反映形式"; tabObj.rows[0].cells[3].colSpan =2; tabObj.rows[0].cells[3].innerHTML="待处理业务数"; tabObj.rows[0].cells[4].colSpan =1; tabObj.rows[0].cells[4].innerHTML="反映形式"; tabObj.rows[0].cells[5].colSpan=1; tabObj.rows[0].cells[5].innerHTML="反映形式"; tabObj.rows[0].cells[6].colSpan=7; tabObj.rows[0].cells[6].innerHTML=""; tabObj.rows[0].cells[7].style.display = "none"; tabObj.rows[0].cells[8].style.display = "none"; tabObj.rows[0].cells[9].style.display = "none"; tabObj.rows[0].cells[10].style.display = "none"; tabObj.rows[0].cells[11].style.display = "none"; tabObj.rows[0].cells[12].style.display = "none"; tabObj.rows[0].cells[13].style.display = "none"; tabObj.rows[0].cells[14].style.display = "none"; } </script> <script language="javascript" type="text/javascript"> document.onreadystatechange = subSomething; function subSomething() { if(document.readyState == "complete") maketotal(); } </script> </head> <body> <input type="button" onclick="maketotal()" value="遍历table"/> <p class=pageNext> <h1 id="FONT-nbsp-style-FONT-FAMILY-nbsp-隶书-nbsp-FONT-SIZE-nbsp-px-反映来源统计-FONT"><FONT style="FONT-FAMILY: 隶书; FONT-SIZE: 34px">反映来源统计</FONT></h1> <TABLE border="1" id="printtable" style="TEXT-ALIGN: center; BORDER-COLLAPSE: collapse" class=di width="100%"> <TBODY> <TR> <TD>处理分类</TD> <TD>处理内容</TD> <TD>来电数</TD> <TD>来访数</TD> <TD>接办数</TD> <TD>待办数</TD> <TD>回复率</TD> <TD>fffff</TD> <TD>水厂总部</TD> <TD>营业厅</TD> <TD>客户服务中心</TD> <TD>管线部</TD> <TD>信息中心</TD> <TD>稽查科</TD> <TD>水质检测</TD> <TD>安装公司</TD></TR> <TR height=30> <TD>测试分类</TD> <TD>水务局</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD></TR> <TR height=30> <TD>测试分类</TD> <TD>投诉</TD> <TD>0</TD> <TD>0</TD> <TD>40</TD> <TD>2</TD> <TD>95.24</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD></TR> <TR height=30> <TD>测试分类</TD> <TD>网格中心</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD></TR> <TR height=30> <TD>测试分类</TD> <TD>居民用户</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD></TR> <TR height=30> <TD>测试分类</TD> <TD>政府办</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD></TR> <TR height=30> <TD>测试分类</TD> <TD>单位用户</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD></TR> <TR height=30> <TD>合计</TD> <TD></TD> <TD>0</TD> <TD>0</TD> <TD>40</TD> <TD>2</TD> <TD>95.24</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD> <TD>0</TD></TR></TBODY></TABLE></p> </body> </html>
The operation effect diagram is as follows:
Update For examples of methods to dynamically modify tables and merge cells using multiple JS, please pay attention to the PHP Chinese website for related articles!

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

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



Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

The article discusses effective JavaScript debugging using browser developer tools, focusing on setting breakpoints, using the console, and analyzing performance.

This article explores effective use of Java's Collections Framework. It emphasizes choosing appropriate collections (List, Set, Map, Queue) based on data structure, performance needs, and thread safety. Optimizing collection usage through efficient

The article explains how to use source maps to debug minified JavaScript by mapping it back to the original code. It discusses enabling source maps, setting breakpoints, and using tools like Chrome DevTools and Webpack.

This tutorial will explain how to create pie, ring, and bubble charts using Chart.js. Previously, we have learned four chart types of Chart.js: line chart and bar chart (tutorial 2), as well as radar chart and polar region chart (tutorial 3). Create pie and ring charts Pie charts and ring charts are ideal for showing the proportions of a whole that is divided into different parts. For example, a pie chart can be used to show the percentage of male lions, female lions and young lions in a safari, or the percentage of votes that different candidates receive in the election. Pie charts are only suitable for comparing single parameters or datasets. It should be noted that the pie chart cannot draw entities with zero value because the angle of the fan in the pie chart depends on the numerical size of the data point. This means any entity with zero proportion

Once you have mastered the entry-level TypeScript tutorial, you should be able to write your own code in an IDE that supports TypeScript and compile it into JavaScript. This tutorial will dive into various data types in TypeScript. JavaScript has seven data types: Null, Undefined, Boolean, Number, String, Symbol (introduced by ES6) and Object. TypeScript defines more types on this basis, and this tutorial will cover all of them in detail. Null data type Like JavaScript, null in TypeScript
