【发布时间】:2016-06-30 18:44:53
【问题描述】:
我正在尝试实现一个 javascript,它将在单击时突出显示 html 表中的列。作为下面的行突出显示工作示例,我尝试将其与 table.columns 一起使用,但 table.columns 不存在。是有没有使用 jquery 突出显示 html 表中的列?
突出显示行的工作代码: 表高亮POC
<script>
function highlight() {
var table = document.getElementById('dataTable');
for (var i = 0; i < table.rows.length; i++) {
table.rows[i].onclick = function () {
if (!this.hilite) {
this.origColor = this.style.backgroundColor;
this.style.backgroundColor = '#BCD4EC';
this.hilite = true;
}
else {
this.style.backgroundColor = this.origColor;
this.hilite = false;
}
}
}
}
</script>
<style>
table {
border-spacing: 0px;
}
td {
border: 1px solid #bbb;
padding: 0.2em;
}
</style>
</head>
<body>
<table id="dataTable">
<tr onclick="highlight()"><td>Data1</td><td>Data2</td></tr>
<tr onclick="highlight()"><td>Data1</td><td>Data2</td></tr>
<tr onclick="highlight()"><td>Data1</td><td>Data2</td></tr>
</table>
</body>
</html>
【问题讨论】:
-
Here 您可以看到如何选择与单击的同一列中的所有单元格。然后您需要做的就是将 backgroundColor 设置为所有这些单元格。
-
您的问题是关于突出显示列,但您的代码似乎建议尝试选择行。你想做的是哪一个?
标签: javascript jquery html