jQuery怎么获取<c:forEach>标签的值

最近做项目中要获取到<c:forEach>标签遍历出的元素的id,之前都是通过在遍历出的元素中添加onclick()函数,再通过原生的js来触发该函数实现。这次分享以下jQuery的实现。

HTML代码:

<table>
	<tbody>
		<c:forEach items="${map.list }" var="list">
			<tr class="icList" id="${list.icId }">
				<th>${list.icName }</th>
			</tr>
		</c:forEach>
	</tbody>
</table>



jQuery代码:

$(".icList").each(function(){
	$(this).click(function(){
		alert($(this).attr('id'));
	});
});

运行结果:

点击对应的行标签,弹出提示框显示对应元素的id。

猜你喜欢

转载自blog.csdn.net/kyle313606922/article/details/77096886