点击动态生成的表格每行时变色
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>select-row</title>
</head>
<body>
<div>
<table id="tab-show"></table>
</div>
</body>
</html>
let index;
let name;
let tr = $('<tr onclick=selectRow(this)><th>+ index +</th><th title="+ name +"></tr>');
$('#tab-show').append(tr);
// 设置点击表格每行选中变色
let selectTr = null;
function selectRow(obj) {
if (obj) {
if (selectTr) {
selectTr.style.backgroundColor = '';
selectTr = obj;
selectTr.style.backgroundColor = '#4ca9f7';
} else {
selectTr = obj;
selectTr.style.backgroundColor = '#4ca9f7';
}
} else {
if (selectTr) {
selectTr.style.backgroundColor = '';
selectTr = null;
}
}
}