【发布时间】:2016-11-04 12:45:25
【问题描述】:
我已经搜索并尝试了一些 jquery 选择器(例如 :last),但我不知道应该使用哪个选择器。我也应该使用this 吗?问题是,我想在嵌套的<table> 中添加一个row,如果我单击添加按钮,唯一特定的<table> 可以在其中添加一个row。
这是我的示例代码:
index.html ==================================================== ========================
<table>
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>Address</th>
<th>Gender</th>
<th>My Items</th>
</tr>
</thead>
<tbody>
<tr>
<td>Some Name</td>
<td>30</td>
<td>My Address</td>
<td>Male</td>
<td>
<table>
<thead>
<tr>
<th>Brand</th>
<th>Quantity</th>
<th>Size</th>
<th>Color</th>
</tr>
</thead>
<tbody>
<tr>
<td>Yamaha</td>
<td>30</td>
<td>Large</td>
<td>Black</td>
</tr>
</tbody>
</table>
<button id="AddItem">Add Item</button>
</td>
</tr>
</tbody>
</table>
<button id="AddCustomer">Add Customer</button>
- 如果我单击
Add Item,则在 Yamaha 项目下方添加 1 row()。 - 如果我单击
Add Customer,则会在Some Name 下方添加1 row()。
myJS.js 代码:
==================================================== ========================
$("#addItem").on('click', function(e) {
$('tr:last').after("Added Row for Items.");
e.preventDefault();
});
<br>
$("#addCustomer").on('click', function(e) {
$('tr:last').after("Added Row for Customer.");
e.preventDefault();
});
【问题讨论】:
-
当您单击 Add Custome 时,应在包含某个名称的某行之后添加 1 行;?
标签: javascript jquery html this