Two methods: 1. Use append() to add tr sub-elements, the syntax is "$("table").append("
td data ")". 2. Use appendTo(), the syntax is "$("td data ").appendTo("table");".
The operating environment of this tutorial: windows7 system, jquery1.10.2 version, Dell G3 computer.
Adding a row of data to the table is to add the tr sub-element to the table element. The following are two methods:
Method 1: append() method
append( ) method inserts content to the "end" inside the selected element.
$(A).append(B)
means inserting B at the end of A.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="js/jquery-1.10.2.min.js"></script> <script> $(document).ready(function() { $("button").on("click", function() { $("table").append("<tr><td>数据</td><td>数据</td></tr>"); }); }); </script> </head> <body class="ancestors"> <table border="1"> <tr> <th>商品</th> <th>价格</th> </tr> <tr> <td>T恤</td> <td>¥100</td> </tr> <tr> <td>牛仔褂</td> <td>¥250</td> </tr> <tr> <td>牛仔库</td> <td>¥150</td> </tr> </table><br> <button>增加一行表格</button> </body> </html>
Method 2: appendTo( ) method
appendTo( ) and append( ) Although the functions of these two methods are similar, Both insert content "at the end" inside the selected element, but the operating objects of the two are reversed.
$(A).appendTo(B)
$(A).appendTo(B) means inserting A into the end of B.
<script> $(document).ready(function() { $("button").on("click", function() { $("<tr><td>数据</td><td>数据</td></tr>").appendTo("table"); }); }); </script>
[Recommended learning: jQuery video tutorial, web front-end video】
The above is the detailed content of How to add a row of tables in jquery. For more information, please follow other related articles on the PHP Chinese website!