站长资讯网
最全最丰富的资讯网站

jquery怎么判断点击的列是第几列

方法:1、用“元素.click(function(){})”给元素绑定点击事件,并设置处理函数;2、在函数中,用“$(this).index()+1”语句获取点击元素的行数即可。index()获取的元素位置是从0开始计数的,需进行加1处理。

jquery怎么判断点击的列是第几列

本教程操作环境:windows7系统、jquery1.10.2版本、Dell G3电脑。

jquery判断点击的列是第几列

实现思想:

  • 用click()给元素绑定点击事件,并设置事件处理函数

  • 在处理函数中,用index()获取点击元素位置,获取点击元素的行数即可

    注:index()获取的元素位置是从0开始计数的,需进行加1处理。

实现代码:

<!DOCTYPE html> <html> 	<head> 		<meta charset="utf-8"> 		<script src="js/jquery-1.10.2.min.js"></script> 		<style> 			tr:nth-of-type(even) { 				background-color: red; 			} 			tr:nth-of-type(odd) { 				background-color: pink; 			} 		</style> 		<script> 			$(document).ready(function() { 				$("tr").click(function() { 					 					console.log("是表格的第 "+($(this).index()+1)+" 列"); 				}); 			}); 		</script> 	</head> 	<body> 		<table border="1"> 			<tr> 				<th>商品</th> 				<th>价格</th> 			</tr> 			<tr> 				<td>T恤</td> 				<td>¥100</td> 			</tr> 			<tr> 				<td>牛仔褂</td> 				<td>¥250</td> 			</tr> 			<tr> 				<td>牛仔库</td> 				<td>¥150</td> 			</tr> 		</table> 		<p>判断点击列是表格的第几列</p>  	</body> </html>

jquery怎么判断点击的列是第几列

说明:

index() 方法获取的元素位置是从0开始的,因此想要获取准确的行数,需要进行计算,在获取值的基础上加1。

【推荐学习:jQuery视频教程、web前端视频】

赞(0)
分享到: 更多 (0)

网站地图   沪ICP备18035694号-2    沪公网安备31011702889846号