zoukankan      html  css  js  c++  java
  • document基本操作 动态脚本-动态样式-创建表格

    var html = document.documentElement;
    var body = document.body;
    
    window.onload = function() {
        //document.write("Hello world!");
    }
    var fragment = document.createDocumentFragment();
    var ul = document.getElementById("myDiv");
    var li = null;
    
    for (var i = 0; i < 3; i++) {
        li = document.createElement("li");
        li.appendChild(document.createTextNode("Item" + (i + 1)));
        fragment.appendChild(li);
    }
    
    ul.appendChild(fragment);
    
    //动态创建脚本
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src = "docment.js";
    document.body.appendChild(script);
    
    //var attr = document.createAttribute("align");
    //动态样式
    
    var link = document.createElement("link");
    link.rel = "stylesheet";
    link.type = "text/css";
    link.href = "style.css";
    
    var head = document.getElementsByTagName("head")[0];
    head.appendChild(link);
    
    //表格
    var table = document.createElement("table");
    table.border = 1;
    table.width = "100%";
    //创建tbody
    var tbody = document.createElement("tbody");
    table.appendChild(tbody);
    //创建行
    var row1 = document.createElement("tr");
    tbody.appendChild(row1);
    
    var cell1 = document.createElement("td"); //创建列
    cell1.appendChild(document.createTextNode("cell1 1.1")); //设置列文本内容
    row1.appendChild(cell1); //设置列所属行
    
    var cell2 = document.createElement("td");
    cell2.appendChild(document.createTextNode("cell2 2.1"));
    row1.appendChild(cell2);
    
    document.body.appendChild(table);
    
    
    //通过属性方法创建表格
    
    var table = document.createElement("table");
    table.border = 1;
    table.width = "100%";
    
    var tbody = document.createElement("tbody");
    table.appendChild(body);
    
    //创建第一行
    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);
    

      

  • 相关阅读:
    git this exceeds GitHub's file size limit of 100.00 MB
    使用vue-cli创建vue工程
    【转】Visual Studio Code必备插件
    linux安装openssl
    Centos7离线安装mysql8
    使用nmon来按频率采集数据
    Mac下编译android4.0.4遇到的问题
    32位ubuntu16.4编译android4.1.1
    vmvare安装vmtools菜单灰色
    Substrate 使用
  • 原文地址:https://www.cnblogs.com/ms_senda/p/11466397.html
Copyright © 2011-2022 走看看