如何根据类名和可编辑的值切换div背景颜色

我想基于类名称“ cls-editable”更改div背景颜色,javascript使用它来查找元素,然后将editable属性设置为“ true”或“ false”。

可编辑时,背景为黄色。否则,它是白色的。

HTML

<div class='cls-editable'>
  Hello
</div>


CSS:

.cls-editable,[contenteditable="true"] {
   background-color: yellow;
}

.cls-editable,[contenteditable="false"] {
   background-color: white;
}


Javascript:

if (this.checkStatus) {
  $('.dirs_row').children('.cls-editable').each(function () {
    $(this).attr('contenteditable','true');
  });
} else {
  $('.dirs_row').children('.cls-editable').each(function () {
    $(this).attr('contenteditable','false');
  });
}

但是,它不起作用。 CSS怎么了?

mqm07072009 回答:如何根据类名和可编辑的值切换div背景颜色

尝试一下

CSS

.cls-editable[contenteditable="true"] {
   background-color: yellow;
}

.cls-editable[contenteditable="false"] {
   background-color: white;
}

输出

enter image description here

,

您几乎完全正确!这是正确的解决方案:

for ($i = 1; $i -lt 11; $i ++)
{
    ('First for loop: ' + $i)

    for ($x = 1; $x -lt 11; $x ++)
    {
        ('Second for loop: ' + $x)
    }
}
First for loop: 1
Second for loop: 1
Second for loop: 2
Second for loop: 3
Second for loop: 4
Second for loop: 5
Second for loop: 6
Second for loop: 7
Second for loop: 8
Second for loop: 9
Second for loop: 10
First for loop: 2
Second for loop: 1
Second for loop: 2
...
.cls-editable[contenteditable="true"] {
   background-color: yellow;
}

.cls-editable[contenteditable="false"] {
   background-color: white;
}

本文链接:https://www.f2er.com/3139723.html

大家都在问