Jquery 动态生成表格 并为行绑定单击变色动作

来源:互联网 发布:ubuntu gcc 安装包 编辑:程序博客网 时间:2024/05/22 03:02
<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
$(function(){
 
                /////////////////以下动态生成10行2列的表格
for(i=1;i<=10;i++)
{
$("#mytable").append("<tr><td>&nbsp;</td><td>&nbsp;</td></tr>"); 
}
/////////////表格生成结束

                /////////////为生成的行添加单击变色动作
$"#mytable tr").click(function(){
if($(this).hasClass("redcss"))
{
$(this).siblings("tr").removeClass("redcss");
}
else
{
$(this).addClass("redcss");
$(this).siblings("tr").removeClass("redcss");
}
})

})

</script>
<style>
.redcss{
background-color:#900;
}

</style>
</head>
<body>
<table width="200" border="1" id="mytable"></table>
</table>
</body>
</html>

原创粉丝点击