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);
    

      

  • 相关阅读:
    启动与指定的文件或协议相关联的默认应用程序
    Windows phone msdn 索引
    34、ShareTarget
    36、UI contrast and settings
    Windows 8下默认管理员登录
    精益创业 Lean Startup
    38、animation
    access2003 基础 1008
    JQuery DOM
    用Javascript实现面向对象编程(封装,抽象,继承,多态)
  • 原文地址:https://www.cnblogs.com/ms_senda/p/11466397.html
Copyright © 2011-2022 走看看