zoukankan      html  css  js  c++  java
  • JavaScript DOM操作表格

    DOM


      【1】DOM(Document Object Model)文档对象模型

      【2】当浏览器载入HTML时,会生成相应的DOM树。

    DOM操作表格:

      表格table元素是HTML中最复杂的结构之一。要想创建表格,一般都必须涉及表示表格行、单元格、表头等方面的标签。由于涉及的标签多,因而使用核心DOM方法创建和修改表格往往都免不了要编写大量的代码。

     需求 |

        要通过DOM实现下列格式的表格结构

    <table>
      <tbody>
        <tr>
          <td>Cell 1,1</td>
          <td>Cell 2,1</td>
        </tr>
        <tr>
          <td>Cell 1,2</td>
          <td>Cell 2,2</td>
        </tr>
      </tbody>
    </table>

      DOMcore |

        如果通过DOMcore方法,则方法如下

    //创建表格
    var table = document.createElement("table");
    table.border = "1";
    table.width = "100%";
    
    //创建tbody
    var tbody = document.createElement("tbody");
    table.appendClicd(tbody);
    
    //创建第一行
    var row1 = document.createElement("tr");
    tbody.appendChild(row1);
    var cell1_1 = document.createElement("td");
    cell1_1.appendChild(documnet.createTextNode("Cell 1,1"));
    row1.appendChild(cell1_1);
    var cell2_1 = document.createElement("td");
    cell2_1.appendChild(document.createTextNode("Cell 2,1"));
    row1.appendChild(cell2_1);
    
    //创建第二行
    var row2 = document.createElement("tr");
    tbody.appendChild(row2);
    var cell1_2 = document.createElement("td");
    cell1_2.appendChild(document.createTextNode("Cell 1,2"));
    row2.appendChild(cell1_2);
    var cell2_2 = document.createElement("td");
    cell2_2.appendChild(document.createTextNode("Cell 2,2"));
    row2.appendChild(cell2_2);
    
    //将表格添加到文档主体中
    document.body.appendChild(table);

      属性和方法 |

         显示DOM代码很长,为了方便构建表格,HTML DOM为<table>、<tbody>、<tr>元素添加了属性和方法。

        【1】为<table>元素添加的属性和方法

    caption:保存着对<caption>元素的指针
    tBodies:是一个<tbody>元素的HTMLCollection
    tFoot:保存着对<tfoot>元素的指针
    tHead:保存着对<thead>元素的指针
    createTHead():创建<thead>元素,将其放到表格中,返回引用
    createTFoot():创建<tfoot>元素,将其放到表格中,返回引用
    createCaption():创建<caption>元素,将其放到表格中,返回引用
    deleteTHead():删除<thead>元素
    deleteTFoot():删除<tfoot>元素
    deleteCaption():删除<caption>元素

        【2】为<tbody>元素添加的属性和方法

    rows: 保存着<tbody>元素中行的HTMLCollection
    deleteRow(pos):删除指定位置的行
    insertRow(pos);向rows集合中的指定位置插入一行,返回对新插入行的引用

        【3】为<tr>元素添加的属性和方法

    cells:保存着<tr>元素中单元格的HTMLCollection
    deleteCell(pos):删除指定位置的单元格
    insertCell(pos):向cells集合中的指定位置插入一个单元格,返回对新插入单元格的引用

       代码重写 |

    //创建表格
    var table = document.createElement("table");
    table.border = "1";
    table.width = "100%";
    
    //创建tbody
    var tbody = document.createElement("tbody");
    table.appendChild(tbody);
    
    //创建第一行
    tbody.insertRow(0);
    tbody.rows[0].insertCell(0);
    tbody.rows[0].cells[0].appendChild(document.createTextNode("Cell 1,1"));
    tbody.rows[0].insertCell(1);
    tbody.rows[0].cells[1].appendChild(document.createTextNode("Cell 2,1"));
    
    //创建第二行
    tbody.insertRow(1);
    tbody.rows[1].insertCell(0);
    tbody.rows[1].cells[0].appendChild(document.createTextNode("Cell 1,2"));
    tbody.rows[1].insertCell(1);
    tbody.rows[1].cells[1].appendChild(document.createTextNode("Cell 2,2"));
    
    //将表格添加到文档主体中
    document.body.appendChild(table);

        效果展示 |

    <script>
    //创建表格
    var table = document.createElement("table");
    table.border = "1";
    table.width = "100%";
    
    //创建tbody
    var tbody = document.createElement("tbody");
    table.appendChild(tbody);
    
    //创建第一行
    tbody.insertRow(0);
    tbody.rows[0].insertCell(0);
    tbody.rows[0].cells[0].appendChild(document.createTextNode("Cell 1,1"));
    tbody.rows[0].insertCell(1);
    tbody.rows[0].cells[1].appendChild(document.createTextNode("Cell 2,1"));
    
    //创建第二行
    tbody.insertRow(1);
    tbody.rows[1].insertCell(0);
    tbody.rows[1].cells[0].appendChild(document.createTextNode("Cell 1,2"));
    tbody.rows[1].insertCell(1);
    tbody.rows[1].cells[1].appendChild(document.createTextNode("Cell 2,2"));
    
    //将表格添加到文档主体中
    document.body.appendChild(table);
    </script>

     

    转载出处:小火柴的蓝色理想

  • 相关阅读:
    SQL Server-数据库架构和对象、定义数据完整性
    SQL Server 2014 中,新建登录用户,分配权限,并指定该用户的数据
    SQL Server SQL性能优化之--数据库在“简单”参数化模式下,自动参数化SQL带来的问题
    SQL Server-简单查询语句,疑惑篇
    SQL Server-聚焦聚集索引对非聚集索引的影响
    SQL Server-聚焦使用索引和查询执行计划
    SQL Server-聚焦移除Bookmark Lookup、RID Lookup、Key Lookup提高SQL查询性能
    SQL SERVER中的sys.objects和sysobjects的区别
    详解sqlserver查询表索引
    双系统如何正确的使用修复BCD工具分享
  • 原文地址:https://www.cnblogs.com/wssjzw/p/9367070.html
Copyright © 2011-2022 走看看