[英]element switch class but don't get styles of the new class
I'm working on a real-life timetable where you can watch where the students current be.我正在制定一个真实的时间表,您可以在其中查看学生当前的位置。 They have six meetings a day.
他们每天有六次会议。 if a student exit the scool i want to delete him from the list.
如果学生退出学校,我想将他从列表中删除。 if i doubleclick the name i want to change the class from name to selected and change the backgoundcolor so if i would press the "Schüler löschen"-button all selected get deleted.
如果我双击名称,我想将 class 从名称更改为选中并更改背景颜色,所以如果我按下“Schüler löschen”按钮,所有选中的都将被删除。
Here is the code:这是代码:
function createTable(){
var table = document.getElementById('table');
for(var j = 0;j<names.length;j++) {
var row = document.createElement("tr");
row.classList.add('row');
for(i=0;i<8;i++){
if(i==0){
var cell = document.createElement("input");
cell.classList.add('name');
cell.value= names[j];
cell.addEventListener("dblclick", function(){
alert(cell.classList);
cell.classList.add('selected');
alert(cell.classList);
});
}else if(i==4){
var cell = document.createElement("td");
cell.classList.add('spacer');
}else{
var cell = document.createElement("td");
cell.classList.add('cell');
cell.textContent = '';
}
row.appendChild(cell);
}
table.children[0].appendChild(row);
}
}
createTable();
the css: css:
input.name {
width: 20em;
background-color: #00ff00;
}
input.selected{
background-color: #ff7f7f;
}
The problem is if i double click the field the color dosent switch to red it stays green but the alert()-function shows that the class has switched问题是,如果我双击该字段,颜色会切换为红色,它会保持绿色,但 alert() 函数显示 class 已切换
i've also tried我也试过
cell.name {
width: 20em;
background-color: #00ff00;
}
cell.selected{
background-color: #ff7f7f;
}
You need to remove cell
class, and use this in that scope.您需要删除
cell
class,并在 scope 中使用它。
Try this:尝试这个:
function createTable() { var table = document.getElementById('table'); var names = ["TEST 1", " TEST 2"]; for (var j = 0; j < names.length; j++) { var row = document.createElement("tr"); row.classList.add('row'); for (i = 0; i < 8; i++) { if (i == 0) { var cell = document.createElement("input"); cell.classList.add('name'); cell.value = names[j]; cell.addEventListener("dblclick", function () { alert(cell.classList); this.classList.remove('cell'); this.classList.add('selected'); alert(cell.classList); }); } else if (i == 4) { var cell = document.createElement("td"); cell.classList.add('spacer'); } else { var cell = document.createElement("td"); cell.classList.remove('selected'); cell.classList.add('cell'); cell.textContent = ''; } row.appendChild(cell); } table.children[0].appendChild(row); } } createTable();
input.name { width: 20em; background-color: #00ff00; } input.selected{ background-color: #ff7f7f; }
<table id="table"><tbody></tbody></table>
I didn't get why cell
is input element
and why do you need to added in tr
我不明白为什么
cell
是input element
以及为什么需要在tr
中添加
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.