<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>JQuery 行点击</title>
<style type="text/css">
.yelBG{ background-color:Yellow;}
</style>
<script src="js/jquery-1.5.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
//页面html代码解析完后,为每个行添加 鼠标移入事件 方法
$("#t1 tr").mouseover(function () {
$(this).css("cursor", "pointer"); //将鼠标改成 手型
}).click(function () {//(链式操作)同时再为每个行添加 点击事件 方法
//为当前点击的行 添加样式表 yelBG,然后为它的兄弟元素移除 yelBG 样式
$(this).addClass("yelBG").siblings().removeClass("yelBG");
var nowIndex = $("#t1 tr").index($(this)); //获得当前选中行的下标
alert(nowIndex);
});
//获得焦点时添加样式
$("input").focus(function () {
$(this).addClass("yelBG");
}).blur(function () {//(链式操作)失去焦点时 移除样式
$(this).removeClass("yelBG");
});
})
</script>
</head>
<body >
<table id="t1" style="width: 100%;">
<tr><td>1</td><td>2</td></tr>
<tr><td>1</td><td>2</td></tr>
<tr><td>1</td><td>2</td></tr>
<tr><td>1</td><td>2</td></tr>
<tr><td>1</td><td>2</td></tr>
</table>
<input type="text" /><input type="text" />
</body>
</html>