js_table动态增加和删除

html页面

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>表格练习</title>
    <style type="text/css">
        #table {
            width: 450px;
            height: 150px;
            border: #0C9 1px solid;
            border-collapse: collapse;

        }

        .button:hover {
            background-color: #4CAF50;
        }

        td:hover {
            background-color: goldenrod;
        }
    </style>

</head>
<body>
    <h1>表格生成练习</h1>

        行数: <input type="number" id="row" value="3">
        列数: <input type="number" id="column" value="4">
        <button type="reset" id="Reset" class="button" onclick="reset()">重置</button>
        <button type="button" class="button" onclick="generat()">生成表格</button>

    <hr>
    表格1
    <button type="button" class="addRow" onclick="addRow(‘rowIndex‘)">插入行</button>
    <button type="button" class="delRow" onclick="delRow(‘rowIndex‘)">删除行</button>
    <button type="button" class="addCol" onclick="addCol(‘colIndex‘)">插入列</button>
    <button type="button" class="delCol" onclick="delCol(‘colIndex‘)">删除列</button>
    <button type="button" class="recolor" onclick="reCol(‘rowIndex‘,‘colIndex‘)">重新着色</button>
    <div id="d1" type="text"></div>

    <script type="text/javascript" src="JavaScript.js"></script>
    <script type="text/javascript" src="column.js"></script>
    <script type="text/javascript" src="rows.js"></script>
</body>

</html>

js-add/del-row-col页面

var table = null;
//生成表格
function generat() {
    //创建table表格

     //refresh
   //document.getElementById("d1").innerHTML = "";
    //获取行数值
    var row = document.getElementById("row").value;
    if (row == "") {
        alert("row is null");
    }
    //获取列数值
    var column = document.getElementById("column").value;
    if (column == "") {
        alert("column is null");
    }

    var table = document.createElement("table");
    table.setAttribute("id", "table");
    table.setAttribute("border", "1");
    table.setAttribute("cellspacing", "0");

    for (var i = 0; i < row; i++) {
        //alert(rang);
        //创建tr
        var tr = document.createElement("tr");
        for (var j = 0; j < column; j++) {

            //创建td
            var td = document.createElement("td");
            //td.setAttribute("onclick","display(this)");
            var txt = document.createTextNode(i);
            td.appendChild(txt);
            tr.appendChild(td);

        }
        table.appendChild(tr);
    }
    document.getElementById("d1").appendChild(table);
    //添加监听器
    table.addEventListener("click", display, false);
} (generat());

//获取鼠标点击的行和列
function display(event) {

    var colIndex = null;
    var rowIndex = null;
    var element = event.srcElement || event.target;
    if (!element) return;
    if (element.tagName != "TD") return;
    colIndex = element.cellIndex;
    rowIndex = element.parentNode.rowIndex;
    console.log("row:" + rowIndex + ", col:" + colIndex);
    return rowIndex, colIndex;

    //if (rowCount == undefined)
    //{
    //    colCount = colCount - 1;
    //    var tdObj = event.srcElement;
    //    var trObj, tableObj;
    //    while(colCount > 0)
    //    {
    //        colCount--;
    //    }

    //    while (tdObj.tagName != "TD")
    //    {
    //        tdObj = tdObj.parentNode;
    //    }
    //    trObj = tdObj.parentNode;
    //    tableObj = trObj.parentNode;
    //    if (tableObj.tagName != "table")
    //    {
    //        tableObj = tableObj.parentNode;
    //    }
    //    rowCount = trObj.rowIndex

    //}  

    //table = document.createElement("table");
    //event.stopPropagation;

}

//删除row
function delRow(rowIndex) {
    table = document.getElementById("table");

    if (!table) {
        alert("此table不存在");
        return 0;
    }
    if (!isNaN(rowIndex)) {
        alert("请选择要删除的行");
    }
    else {
        confirm("删除该行??");
        table.deleteRow(rowIndex);

    }
}

//删除列
function delCol(colIndex) {
    table = document.getElementById("table");
    if (!table) {
        alert("此tr不存在");
        return 0;
    }
    if (!isNaN(colIndex)) {
        alert("请选择要删除的列");
    }
    else {
        confirm("删除该列?");
        for (var i = 0; i < table.rows.length; i++) {
            table.rows[i].deleteCell(colIndex);
        }

    }
}
//插入行
function addRow(rowIndex) {
    table = document.getElementById("table");

    if (!table) {
        alert("此table不存在");
        return 0;
    }
    if (!isNaN(rowIndex)) {
        alert("请选择要删除的行");
    }
    else {
        confirm("插入行??");
        var tr = document.createElement("tr");
        for (var j = 0; j < table.rows[0].cells.length; j++) {

            //创建td
            var td = document.createElement("td");
            td.style.backgroundColor = "green";
            tr.appendChild(td);
        }
        table.appendChild(tr);

        ////api 自有方法

        ////var x = row.insertCell(0);
        ////var y = row.insertCell(1);
        ////x.innerHTML = "0";
        ////y.innerHTML = "1";
        //var row = table.insertRow(rowIndex);
        //for (var k = 0; k < table.rows[0].cells.length; k++) {

        //    var cell = row.insertCell();
        //    cell.innerHTML = "addrow";
        //}

    }
}

//插入列
function addCol(colIndex) {
    table = document.getElementById("table");
    if (!table) {
        alert("此tr不存在");
        return 0;
    }
    if (!isNaN(colIndex)) {
        alert("请选择要插入的列");
    }
    else {
        confirm("插入该列?");
        for (var i = 0; i < table.rows.length; i++) {
            table.rows[i].insertCell(colIndex).style.backgroundColor="red";

        }

    }
}

////重置
function reset()
{

    document.getElementById("row").value = null;
    document.getElementById("column").value = null;
    document.getElementById("d1").innerHTML = null;

}

js-- mouse调整行row

var table = document.getElementById("table");
//var headerRows = [];
//for (var i = 1; i < tblObj.rows.length; i++) {
//    headerRows[i] = tblObj.rows[i].cells[0];
//}
var mousedown = false;
var resizeable = false;
var targetTd;
var screenYStart = 0;
var tdHeight = 0;
var headerHeight = 0;
//for (var i = 1; i < headerRows.length; i++) {
//    //添加头部单元格事件
//    addListener(headerRows[i], "mousedown", onmousedown);
//    addListener(headerRows[i], "mousemove", onmousemove);
//}
table.addEventListener("mousedown", onmousedown);
table.addEventListener("mousemove", onmousemove);

function onmousedown(event) {
    console.log(event.target + onmousedown);
    if (resizeable == true) {
        var evt = event || window.event;
        mousedown = true;
        screenYStart = evt.screenY;
        tdHeight = targetTd.offsetHeight;
        headerHeight = table.offsetHeight;
    }
}
function onmousemove(event) {
    console.log(event.target + onmousemove);
    var evt = event || window.event;
    var srcObj = getTarget(evt);
    var offsetY = evt.offsetY
    if (mousedown == true) {
        var height = (tdHeight + (evt.screenY - screenYStart)) + "px";//计算后的新的宽度
        targetTd.style.height = height;
        table.style.height = (headerHeight + (evt.screenY - screenYStart)) + "px";
    } else {
        if (srcObj.offsetHeight - evt.offsetY <= 10) {
            targetTd = srcObj;
            resizeable = true;
            srcObj.style.cursor = ‘row-resize‘;//修改光标样式
        } else if (evt.offsetY <= 10) {
            targetTd = table.rows[srcObj.parentNode.rowIndex - 1];
            resizeable = true;
            srcObj.style.cursor = ‘row-resize‘;
        } else {
            resizeable = false;
            srcObj.style.cursor = ‘default‘;
        }
    }
}
document.onmouseup = function (event) {
    console.log(event.target + onmouseup);
    tartgetTd = null;
    coltargetTd = null;
    resizeable = false;
    mousedown = false;
    colmousedown = false;
    colresizeable = false;
    document.body.style.cursor = ‘default‘;
}

function getTarget(evt) {
    return evt.target || evt.srcElement;
}
function addListener(element, type, listener, useCapture) {
    element.addEventListener ? element.addEventListener(type, listener, useCapture) : element.attachEvent("on" + type, listener);
}

js-- mouse调整列 col

var tTD;
var table = document.getElementById("table");
//bianli row
//for (var i = 0; i < table.rows.length; i++) {
//    //bianli row--col
//    for (var j = 0; j < table.rows[i].cells.length; j++) {
        table.onmousedown = function () {
            //记录单元格
            tTD = this;
            if (event.offsetX > tTD.offsetWidth - 10) {
                console.log(event.target + onmousedown);
               // this.style.cursor = ‘col-resize‘;
                tTD.mouseDown = true;
                tTD.oldX = event.x;
                tTD.oldWidth = tTD.offsetWidth;
            }
            //if (event.offsetY > event.offsetHeight-10) {
            //    tTD.mouseDown = true;
            //    var tHeight = event.offsetHeight;
            //    var tabHeight = table.offsetHeight;
            //}
            //记录Table宽度
            //table = tTD; while (table.tagName != ‘TABLE‘) table = table.parentElement;
            //tTD.tableWidth = table.offsetWidth;
        };
        table.onmouseup = function () {
            //结束宽度调整
            if (tTD == undefined) tTD = this;
            tTD.mouseDown = false;
            tTD.style.cursor = ‘default‘;
        };
        table.onmousemove = function () {
            //更改鼠标样式
            if (event.offsetX > this.offsetWidth - 10)
                this.style.cursor = ‘col-resize‘;
            else
                this.style.cursor = ‘default‘;
            //取出暂存的Table Cell
            if (tTD == undefined) tTD = this;
            //调整宽度
            if (tTD.mouseDown != null && tTD.mouseDown == true) {
                tTD.style.cursor = ‘default‘;
                if (tTD.oldWidth + (event.x - tTD.oldX) > 0)
                    tTD.width = tTD.oldWidth + (event.x - tTD.oldX);
                //调整列宽
                tTD.style.width = tTD.width;
                tTD.style.cursor = ‘col-resize‘;
                //调整该列中的每个Cell
                table = tTD; while (table.tagName != ‘TABLE‘) table = table.parentElement;
                ///

                for (j = 0; j < table.rows.length; j++) {
                    table.rows[j].cells[tTD.cellIndex].width = tTD.width;
                }
                //调整整个表
                //table.width = tTD.tableWidth + (tTD.offsetWidth – tTD.oldWidth);
                //table.style.width = table.width;
            }
        };
//    }
//}
时间: 2024-10-15 11:27:30

js_table动态增加和删除的相关文章

jquery动态增加或删除tr和td【实际项目】

难点: (1)动态增加.删除tr和td (2)每天tr和td都有下标,且下标要动态变化, (3)tr和td为什么下标不能随便写,原因是此处需要把所有tr中的数据以list的形式发送到后台对象中,所有每天tr中的tr中name='对象[index].属性',必须有下标且下标要从0开始且要连续不能跳跃 (4)增加tr和td需要学习细节有: 创建td   var $tdName = $("<td class='in-ctt'></td>"); td添加内容   $td

开机状态下,动态增加与删除SCSI磁盘

开机状态下,SCSI磁盘动态增加与删除命令如下,已经在虚拟机opensuse15.1与物理机centos7.3上测试通过. 删除磁盘命令: echo 1 > /sys/block/sda/device/delete    //sda可以是任何SCSI设备 增加磁盘命令: echo "- - -">/sys/class/scsi_host/host0/scan   //其中host0为磁盘所在的adapter名称 原文地址:https://www.cnblogs.com/lo

hdfs以及hbase动态增加和删除节点

一个知乎上的问题:Hbase的Region server和hadoop的datanode是否可以部署在一台服务器上?如果是的话,二者是否是一对一的关系?部署在同一台服务器上,可以减少数据跨网络传输的流量. 但不是一对一对的关系,首先,数据还hdfs中保存N份,默认是三分,也就是说数据会分布在三个datanode上,即使regionserver上只保存一个region,它也可以与三个datanode交互,更何况,一台regionserver可以保存多个region. 参考:https://www.

jQuery实现表格行的动态增加与删除 序号 从 1开始排列

<table id="tab" border="1" width="60%" align="center" style="margin-top:20px"> <tr> <td width="20%">序</td> <td >标题</td> <td >描述</td> <td >操作&

jQuery实现表格行的动态增加与删除

删除之前删除2行后: 1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2<html xmlns="http://www.w3.org/1999/xhtml"> 3<head> 4<meta http-equiv

DOM增删操作(select动态增加和删除以及清空)

<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title></title> <script> //创建select元素 function createSelect(){ var ele = document.createElement('select'); for(var i=0;i<10;i++){ var op = new Optio

动态增加,删除表格行并按照表格列排序

接着上一篇博文,解决上一篇博文最后遗留的瑕疵,同时,给表格列添加了排序功能. 解决掉的问题 1 乱码的问题 2 删除方法更加简便,省去获取父节点id的代码 3 点击第一行的add按钮后,下行table行变宽问题 <html> <head> <title>Table</title> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"&g

数组中元素的动态增加和删除

1 <div class="ser-pup-line1"> 2 <div class="ser-pup-left"> 3 <span>使用配件</span> 4 </div> 5 <div class="ser-pup-right1"> 6 <div class="clone"> 7 <div class="ser-pup-ad

jquery.validate 动态增加、删除规则

<form name="form" id="form1"> <table> <tr> <td>类型:</td> <td> <select name="type" id="type"> <option value="">请选择</option> <option value="1"