jquery 点击某一行,得到这一行的每个列的数据

<html>
<head>
<title>test</title>

<script src="../Scripts/jquery-1.8.3.js"></script>
<style type="text/css">
table
{
border-collapse: collapse;
border-spacing: 0;
margin-right: auto;
margin-left: auto;
width: 800px;
}
th, td
{
border: 1px solid #b5d6e6;
font-size: 12px;
font-weight: normal;
text-align: center;
vertical-align: middle;
height: 20px;
}
th
{
background-color: Gray;
}

.hover
{
background-color: #cccc00;
}
</style>
<script type="text/javascript" >
$(document).ready(function () {
//鼠标移动到行变色,单独建立css类hover
//tr:gt(0):表示获取大于 tr index 为0 的所有tr,即不包括表头
$("#table3 tr:gt(0)").hover(
function () { $(this).addClass("hover") },
function () { $(this).removeClass("hover") })
});
//$(document).ready(function () {
// $("#table3 tr").click(function () {
// // // alert($(this).find("td").length);//td长度
// // alert($(this).parent().find("tr").length);//tr长度
// //alert($("#table3").find("tr").length);//tr长度

// //alert($(this).prevAll().length ); //tr所在行数
// TaskType = $(this).find("td").eq(0).text(); //

// alert(TaskType);
// });

//});
$(document).ready(function () {
//点击#table3 的单元格返回 单元格索引
$("#table3 td").click(function () {
// alert($(this).text);
var tdSeq = $(this).parent().find("td").index($(this));//当前列索引
var trSeq = $(this).parent().parent().find("tr").index($(this).parent());//当前行索引

// TaskType = $(this).parent().find("td").eq(tdSeq).text(); //用find()
TaskType = $(this).parent().children().eq(tdSeq).text(); //用children()
alert(TaskType);


// alert("第" + (trSeq) + "行," + (tdSeq + 1) + "列");

})
});


</script >
</head >
<body >
<table id="table3">
<tr >
<th style="width: 160px;" > 表头一</th >
<th style="width: 160px;" > 表头二 </th >
<th style="width: 160px;" > 表头三</th >
<th style="width: 160px;" > 表头四</th >
<th style="width: 160px;" > 表头五</th >
</tr >
<tr > <td > 第一行第一列</td > <td > 第一行第二列</td > <td > 第一行第三列</td > <td > 第一行第四列</td > <td > 第一行第五列</td > </tr >
<tr > <td > 第二行第一列</td > <td > 第二行第二列</td > <td > 第二行第三列</td > <td > 第二行第四列</td > <td > 第二行第五列</td > </tr >
<tr > <td > 第三行第一列</td > <td > 第三行第二列</td > <td > 第三行第三列</td > <td > 第三行第四列</td > <td > 第三行第五列</td > </tr >
<tr > <td > 第四行第一列</td > <td > 第四行第二列</td > <td > 第四行第三列</td > <td > 第四行第四列</td > <td > 第四行第五列</td > </tr >
</table >
</body >
</html >

猜你喜欢

转载自www.cnblogs.com/asdyzh/p/9775027.html