简体   繁体   中英

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 :

<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 .

Please tell me what is the problem here.

Instead if the onload event use jquery's 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.

You can find the solution here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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