简体   繁体   English

在Javascript的HTML表格中动态添加表格行

[英]dynamically add table row in HTML Table in Javascript

I have following code: 我有以下代码:

<body onload="LoadData()">
    <table id="myTable">
    </table>
</body>

And I have Javascript function like : 我有像这样的Javascript函数:

<script type="text/javascript">
    function LoadData()
    {
        var sTempTableRow='<tr><td>cell-1 </td><td>cell-2 </td><td>cell-3 </td><td>cell-4 </td></tr>'
        $('#myTable').append(sTempTableRow);
    }
</script>

I have tried numerous times and at all the times face an exception like Microsoft JScript runtime error: Object expected . 我已经尝试了无数次,而且每次都遇到Microsoft JScript runtime error: Object expected类的异常Microsoft JScript runtime error: Object expected

Please tell me what is the problem here. 请告诉我这里是什么问题。

Instead if the onload event use jquery's ready: 相反,如果onload事件使用jquery的ready:

<script type="text/javascript">
    $(document).ready(function()
    {
      var sTempTableRow='<tr><td>cell-1 </td><td>cell-2 </td><td>cell-3 </td><td>cell-4 </td></tr>';
      $('#myTable').append(sTempTableRow);
    });
</script>
<body>
    <table id="myTable">
    </table>
</body>

you can find the solution : 您可以找到解决方案:

$(document).ready(function(){
     var sTempTableRow='<tr><td>cell-1 </td><td>cell-2 </td><td>cell-3 </td><td>cell-4 </td></tr>'
     $('#myTable').append(sTempTableRow);
    });

​and remove the onload statement. 并删除onload语句。

You can find the solution here . 您可以在此处找到解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM