实现效果就是点击表格,触发所在行列样式变化,如图所示“
html
<script src="https://cdn.staticfile.org/jquery/2.2.4/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>文档标题</title>
</head>
<body>
<div id="jsContainer">
<table class="game">
<tbody>
<tr><td></td><td></td><td></td><td></td><td class="wrap"></td><td></td><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td><td></td><td class="wrap"></td><td></td><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td><td></td><td class="wrap"></td><td></td><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td><td></td><td class="wrap"></td><td></td><td></td><td></td><td></td></tr>
<tr><td class="wrap"></td><td class="wrap"></td><td class="wrap"></td><td class="wrap"></td><td class="current"></td><td class="wrap"></td><td class="wrap"></td><td class="wrap"></td><td class="wrap"></td></tr>
<tr><td></td><td></td><td></td><td></td><td class="wrap"></td><td></td><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td><td></td><td class="wrap"></td><td></td><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td><td></td><td class="wrap"></td><td></td><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td><td></td><td class="wrap"></td><td></td><td></td><td></td><td></td></tr>
</tbody>
</table>
</div>
</body>
</html>
css
table.game {
font-size: 14px;
border-collapse: collapse;
width: 100%;
table-layout: fixed;
}
table.game td {
border: 1px solid #e1e1e1;
padding: 0;
height: 30px;
text-align: center;
}
table.game td.current{
background: #1890ff;
}
table.game td.wrap{
background: #f5f5f5;
}
js
$('td').click(function(e){
//删除所有的样式
$('td').removeClass()
//获取当前的行列
let col=$(this).parent().index(),row=$(this).index();
//获取所在行
$('tbody tr:eq('+col+')').children().each(function(){
$(this).addClass('wrap',{'background':' #f5f5f5'})
})
//获取所在列
$('tbody tr').each(function(){
$(this).children().eq(row).addClass('wrap',{'background':' #f5f5f5'})
})
//点击单元格样式设为蓝色
$(this).removeClass('wrap')
$(this).addClass('current',{'background':'#1890ff'})
})