我在一个 JSP 文件中有以下的 HTML 代码:
<div class="custList">
<table class="dataGrid">
<c:forEach var="cust" items="${custList}">
<tr>
<td>${cust.number}</td>
<td>${cust.description}</td>
<td>${cust.type}</td>
<td>${cust.status}</td>
</tr>
</c:forEach>
</table>
</div>
我需要能够在动态创建的<tr>
标签上触发'click'
事件,并且还要能够从JavaScript函数内访问所点击<tr>
标签的<td>
标签的值。我已经有了这个函数,但不幸的是它似乎没有起作用。$(document).ready(function() {
$("div.custList > table > tr").live('click', function() {
alert("You clicked my <tr>!");
//get <td> element values here!!??
});
});
更新 (2016年1月):jQuery.live已被弃用(如此处所示:http://api.jquery.com/live/)
从jQuery 1.7开始,.live()方法已被弃用。使用.on()来绑定事件处理程序。
$("div.custList table tr:has(td)").click(function () { //Code here });
这将帮助您跳过第一行表头。 - Max