这是我所拥有的例子,现在我想要的是获取信息,如果检查了行。信息应保存为布尔变量,其名称与突出显示的行的第一列中的文本相同
window.onload = highlight();
function highlight(){
var table = document.getElementById('myTab1');
for (var i=0;i < table.rows.length;i++){
table.rows[i].onclick = function () {
var flag = false;
if(this.className == 'check-tr'){
this.className = 'check-tr-checked';
flag = true;
}
if(this.className == 'check-tr-checked' && flag == false){
this.className = 'check-tr';
}
}
}
}
.check-td{
background-color: #FFFFFF;
padding: 8px;
border: 1px solid;
min-width:100px;
}
.check-tr .check-td{
background-color: #ffffff;
padding: 8px;
border: 1px solid;
min-width:100px;
}
.check-tr:hover .check-td{
background: #ddd;
}
.check-tr-checked .check-td{
background-color: #ababad;
padding: 8px;
border: 1px solid;
min-width:100px;
}
.check-tr-checked:hover .check-td{
background: #ddd;
}
<table id="myTab1" style="width:90%">
<thead>
<td class="report-th" colspan="2">
Reports
</td>
</thead>
<tbody>
<tr class="check-tr">
<td class="check-td">
a
</td>
<td class="check-td">
1
</td>
</tr>
<tr class="check-tr">
<td class="check-td">
b
</td>
<td class="check-td">
2
</td>
</tr>
<tr class="check-tr">
<td class="check-td">
c
</td>
<td class="check-td">
3
</td>
</tr>
<tr class="check-tr">
<td class="check-td">
d
</td>
<td class="check-td">
4
</td>
</tr>
</tbody>
</table>
这是我的html项目的一部分,它使用JavaScript来检查和取消选中表中的行(更改其类)
window.onload = highlight();
function highlight{
var table = document.getElementById('myTab1');
for (var i=0;i < table.rows.length;i++){
table.rows[i].onclick = function () {
alert(this.content);
var flag = false;
if(this.className == 'check-tr'){
this.className = 'check-tr-checked';
flag = true;
}
if(this.className == 'check-tr-checked' && flag == false){
this.className = 'check-tr';
}
}
}
}
我想将单击的第一个单元格的文本存储到变量中。有人可以帮我吗?
答案 0 :(得分:1)
window.onload = highlight();
function highlight(){ //added paranthesis here
var table = document.getElementById('myTab1');
for (var i=0;i < table.rows.length;i++){
table.rows[i].onclick = function () {
alert(this.innerText);
var flag = false;
if(this.className == 'check-tr'){
this.className = 'check-tr-checked';
flag = true;
}
if(this.className == 'check-tr-checked' && flag == false){
this.className = 'check-tr';
}
}
}
}