有一个列出用户的网格(只是html表),您可以通过单击删除链接删除特定用户.我通常的做法是
<% foreach (var user in Model.Users) {%>
<tr >
<td align="right"><%= user.Name %></td>
<td><%= user.Level %></td>
<td align="center">
<a href="#" onclick="return deleteUser('<%= user.Name %>');">
<%= Html.Image("trash.gif") %>
</a>
</td>
</tr>
<% )%>
Run Code Online (Sandbox Code Playgroud)
但我想以非突兀的方式将点击事件附加到链接.我的意思是,我不想在标签内指定javascript方法.我不确定用jQuery实现它的最佳方法是什么,用参数传递绑定多个多个锚标签.
您可以delegate()在表上使用该方法:
$('#tableId').delegate('a', 'click', function(e) {
e.preventDefault();
// get the user name
deleteUser($(this).closest('tr').children(':first').text());
// or give the cell that contains the name a class
// deleteUser($(this).closest('tr').children('.name').text());
});
Run Code Online (Sandbox Code Playgroud)
这样,您只需注册一个事件处理程序.
| 归档时间: |
|
| 查看次数: |
1728 次 |
| 最近记录: |