我有一个包含3行的简单表(不包括标题),当点击某行时会生成事件点击并且文本"单击!"打印出来。我需要在单击一行时打印行内容(两个td),而不是打印"单击!"文本。我怎么能用jquery来做呢?
<html>
<head>
<meta charset="UTF-8">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>
<table id="family" border="1">
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<tr>
<td>John</td>
<td>28</td>
</tr>
<tr>
<td>David</td>
<td>19</td>
</tr>
<tr>
<td>Larry</td>
<td>15</td>
</tr>
</table>
<script>
$(document).ready(function() {
$("#family").delegate("tr", "click", function() {
console.log("click!");
});
});
</script>
</body>
</html>
答案 0 :(得分:1)
$("#family").delegate("tr", "click", function() {
var cellText = "";
$(this).children().each(function() {
cellText = cellText + $(this).html() + " ";
});
});