当前位置:Gxlcms > html代码 > 动态添加修改删除html表格内容_html/css_WEB-ITnose

动态添加修改删除html表格内容_html/css_WEB-ITnose

时间:2021-07-01 10:21:17 帮助过:20人阅读

1.需求

需要实现收银台上添加修改删除商品信息时顾显可以实时看到其变化

2.解决

收银台和顾显通过tcp传输进行数据通信,顾显通过操作html中的表格进行数据添加修改和删除操作

3.代码

mytest.html

Dynamic Table

dynamic_table.js

onerror=handleErrvar txt=""function handleErr(msg,url,l) {    txt="本页中存在错误。\n\n"    txt+="错误:" + msg + "\n"    txt+="URL: " + url + "\n"    txt+="行:" + l + "\n\n"    txt+="点击“确定”继续。\n\n"    alert(txt)    return true}function addRow(name, color, size, unit, discount, count, sum) {    var bodyObj=document.getElementById("goods");    if(bodyObj==null)     {        alert("Body of Table not Exist!");        return;    }    var rowCount = bodyObj.rows.length;    //var cellCount = myarray.length;    var newRow = bodyObj.insertRow(rowCount++);    newRow.insertCell(0).innerHTML=name;    newRow.insertCell(1).innerHTML=color;    newRow.insertCell(2).innerHTML=size;    newRow.insertCell(3).innerHTML=unit;    newRow.insertCell(4).innerHTML=discount;    newRow.insertCell(5).innerHTML=count;    newRow.insertCell(6).innerHTML=sum;}function removeRow(tbodyID, row) {    var bodyObj=document.getElementById(tbodyID);    if(bodyObj==null)     {        alert("Body of Table not Exist!");        return;    }    var nrow = Number(row);    if (nrow <= bodyObj.rows.length)        bodyObj.deleteRow(nrow);    else        alert("nrow is less.");}function modifyRow(tbodyID, row, col, newvalue) {    var nrow = Number(row);    var ncol = Number(col);    var bodyObj=document.getElementById(tbodyID);    if(bodyObj==null)     {        alert("Body of Table not Exist!");        return;    }    try    {        //var tableObj = bodyObj.parentNode;        if (nrow < bodyObj.rows.length && ncol < bodyObj.getElementsByTagName('tr')[nrow].getElementsByTagName('td').length)        {            //这个在ie下可以 在google下不行            //bodyObj.rows(nrow).cells(ncol).innerHTML = newvalue;            //bodyObj.rows[nrow].childNodes[ncol].innerHTML = newvalue;            //这个在ie和google下都可以            document.getElementById(tbodyID).getElementsByTagName('tr')[nrow].getElementsByTagName('td')[ncol].innerHTML = newvalue;        }        else            alert("empty.");    }    catch (err)    {        alert(err.description);    }}function clearRows(tbodyID) {    var bodyObj=document.getElementById(tbodyID);    if(bodyObj==null)     {        alert("Body of Table not Exist!");        return;    }    for (var i = 0; i < bodyObj.rows.length; )        bodyObj.deleteRow(i);}

在win7 ie10和google 下运行正常

4.备注

1.最好不使用windows自带的记事本,会存在编码问题,推荐使用notepad++,编码为utf8 无bom

2.部分js函数会存在ie下可用,google下不可用,这个代码是通用的

版权声明:本文为博主原创文章,未经博主允许不得转载。

人气教程排行