Javscript:动态表创建和添加行 [英] Javscript: Dynamic table creation and adding rows

查看:89
本文介绍了Javscript:动态表创建和添加行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Javascript

Javascript

var t="<table id='datatable'>";
        t+=" <tbody><tr><td>head1</td><td> head </td><td> head 1</td></tr>";
        t+="<tr><td>value from db</tr></td>";
        t+=" </tbody></table>";
        document.getElementById('invitee').innerHTML=t;//div tag





以上是我要创建的代码通过从数据库获取数据的动态表



点击按钮我需要在上表中添加一个新行



Above is my code to create a dynamic table by getting data from database

On clicking a button i need to add a new row to the above table

function addRow() {
            var table = document.getElementById('dataTable');
            var rowCount = table.rows.length;
            var row = table.insertRow(rowCount);
            var cell2 = row.insertCell(0);
            cell2.innerHTML = "SDGF";
            var cell3 = row.insertCell(1 );
            var element2 = document.createElement("input");
            element2.type = "button";
            element2.name = "delete";
            cell3.appendChild(element2);
        }



这是添加行的方法。

此方法在html中创建静态表时工作正常但不是对于上面的那个。我收到一个错误

未捕获的TypeError:无法读取属性''行'的null

任何人请帮我解决这个问题。


this is the method to add a row.
this method works fine while creating a static table in html but not for the above one.I am getting an error
"Uncaught TypeError: Cannot read property 'rows' of null "
Anybody please help me solve this issue.

推荐答案

您知道 getElementById 区分大小写吗?

更改为

Do you know getElementById is case sensitive?
change from
Quote:

var table = document.getElementById('dataTable');

var table = document.getElementById('dataTable');

to

to

var table = document.getElementById('datatable');


这篇关于Javscript:动态表创建和添加行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆