javascript - 在 javascript 中使用 tr id 更新 tr innerHTML

标签 javascript html

<script type="text/javascript">
    Array.prototype.AddRecord = function () {

        document.getElementById('mytable').style.display = "block";
        table
        var table = document.getElementById("mytable");
        var rows = table.rows.length;

        var row = table.insertRow(rows++);
        var cell1 = row.insertCell(0);
        var cell2 = row.insertCell(1);
        var cell3 = row.insertCell(2);
        var cell4 = row.insertCell(3);
        var cell5 = row.insertCell(4);

        //alert(row.id);
        var name = this[0];
        var age = this[1];

        var param = "editrow(\'" + name + "\'," + age + "," + rows + ")";
        var param1 = "deleterow(" + rows + ")";
        var param2 = "Update(\'" + name + "\'," + age + "," + rows + ")";

        cell1.innerHTML = name;
        cell2.innerHTML = age;
        cell3.innerHTML = "<input type='button' value='Edit' id='edit' onclick=" + param + " >";
        cell4.innerHTML = "<input type='button' value='Delete' id='delete' onclick=" + param1 + ">";
        cell5.innerHTML = "<input type='button' value='Update' id='update' onclick=" + param2 + ">";

    }

    function Update(name, age, row) {
        alert(name);
        alert(age);
        alert(row);
        document.getElementById('create').style.display = "none";
        document.getElementById('update').style.display = "block";

    }
</script>

我想根据 tr id 的行 ID 更新行并更新 tr 单元格数据请帮助我,我是新手...提前谢谢...

最佳答案

这就是 jQuery 的问题,您会沉迷于它。无论如何,这是原始的 javascript 解决方案:

此处的关键是为您的行分配 ID,以便稍后您可以获取它。

http://jsfiddle.net/PnMPk/1/ << 工作示例。

updateMethod = function ( nm, age, rowID ){
    //alert( 'Changing Name of :' + rowID );
    var o = getO( rowID );
    o.children[0].innerText = 'Changed';
    o.style.color = 'red';
};

//getO => 
var getO = function( _id ){
  return document.getElementById( _id );
};

Array.prototype.AddRecord = function () {
 getO('mytable').style.display = "block";
        table
        var table = getO("mytable"),
            rows = table.rows.length,
            row = table.insertRow(rows++),
            cell1 = row.insertCell(0),
            cell2 = row.insertCell(1),
            cell3 = row.insertCell(2),
            cell4 = row.insertCell(3),
            cell5 = row.insertCell(4),
            rowID = 'row_'+ table.rows.length;

        row.setAttribute('id', rowID ); //Assign some ID

        var name = this[0],
            age = this[1],
            param = "editrow(\'" + rowID + "\'," + age + "," + rows + ")",
            param1 = "deleterow(" + rowID + ")",
            param2 = "updateMethod(\'" + name + "\'," + age + ",'" + rowID + "')";

        cell1.innerHTML = name;
        cell2.innerHTML = age;
        cell3.innerHTML = "<input type='button' value='Edit' id='edit' onclick=" + param + " >";
        cell4.innerHTML = "<input type='button' value='Delete' id='delete' onclick=" + param1 + ">";
        cell5.innerHTML = "<input type='button' value='Update' id='update' onClick=" + param2 + ">";


    };

['rakesh',26].AddRecord();
['juyal',28].AddRecord();
['Click',26].AddRecord();
['update',28].AddRecord();
['CHANGE_NAME',28].AddRecord();

如果您需要对它进行一些修改,请告诉我。

关于javascript - 在 javascript 中使用 tr id 更新 tr innerHTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18527303/

相关文章:

javascript - AngularJS 没有使用 Websocket 更新数据绑定(bind)

javascript - 返回现有对象的函数设置属性的函数。 JS

javascript - 如何通过 underscoreJS 遍历字母表

css - 为 html5(或任何文档类型)添加文档类型会破坏我的网站

javascript - &lt;!DOCUMENT html> 在JS脚本中是否触发 "use strict"?

html - 将 html 文件链接到子目录中的 css 文件不起作用

javascript - 如何在开始之前加载 HTML5 Canvas 游戏?

javascript - 输入类型 ="file"上的点击事件触发不起作用

javascript - Canvas - 填充弧形/圆的特定区域

html - 将盒子向下放置