风格.backgroundColor不行
style.backgroundColor doesn't work?
试图通过点击单元格来改变它的颜色。
单元格通常是灰色的,第一次点击它们应该变成红色。当我点击一个红色的单元格时,它会再次变成灰色。
function changeColor(cell) {
var red = '#FE2E2E';
var grey = '#E6E6E6';
if (cell.style.backgroundColor == grey) {
cell.style.backgroundColor = red;
} else {
if (cell.style.backgroundColor == red) {
cell.style.backgroundColor = grey;
}
};
};
#table tr td {
width: 20px;
height: 50px;
cursor: pointer;
background-color: #E6E6E6;
border: 1px solid black;
}
<table class="table table-bordered" id="table">
<tbody>
<tr>
<td onclick="changeColor(this)"></td>
<td onclick="changeColor(this)"></td>
<td onclick="changeColor(this)"></td>
<td onclick="changeColor(this)"></td>
<td onclick="changeColor(this)"></td>
</tr>
</tbody>
</table>
我也尝试了.style.bgColor
, rgb
和if (cell.style.backgroundColor ===
,但这也不起作用。单元格背景颜色的值是。写成backgroundColor : " 或。bgColor: undefined.
您从style.backgroundColor
返回的值不太可能与您设置的格式相同;它是浏览器想要的任何格式。
一个最小的改变方法是在元素上存储一个标志(见注释):
function changeColor(cell) {
var red = '#FE2E2E';
var grey = '#E6E6E6';
// Get a flag; will be falsy if not there at all
var flag = cell.getAttribute("data-grey");
if (!flag) {
// Make it grey
cell.setAttribute("data-grey", "true");
cell.style.backgroundColor = red;
} else {
// Not grey, make it red
cell.setAttribute("data-grey", ""); // blank is falsy
cell.style.backgroundColor = grey;
}
}
#table tr td {
width: 20px;
height: 50px;
cursor: pointer;
background-color: #E6E6E6;
border: 1px solid black;
}
<table class="table table-bordered" id="table">
<tbody>
<tr>
<td onclick="changeColor(this)"></td>
<td onclick="changeColor(this)"></td>
<td onclick="changeColor(this)"></td>
<td onclick="changeColor(this)"></td>
<td onclick="changeColor(this)"></td>
</tr>
</tbody>
</table>
…但正确的做法是添加/删除类,并使用相应的CSS样式(见注释):
// See https://developer.mozilla.org/en-US/docs/Web/API/Element/classList for classList info
function changeColor(cell) {
// adds or removes the active class
cell.classList.toggle("active");
}
#table tr td {
width: 20px;
height: 50px;
cursor: pointer;
background-color: #E6E6E6;
border: 1px solid black;
}
/* A class we can toggle to override the color above */
#table tr td.active {
background-color: #fe2e2e;
}
<table class="table table-bordered" id="table">
<tbody>
<tr>
<td onclick="changeColor(this)"></td>
<td onclick="changeColor(this)"></td>
<td onclick="changeColor(this)"></td>
<td onclick="changeColor(this)"></td>
<td onclick="changeColor(this)"></td>
</tr>
</tbody>
</table>
你的代码不工作,因为style
属性没有一个backgroundColor
设置当你的代码第一次运行:style
代表一个元素的内联样式属性,你的元素没有一个开始。当你检查元素的背景是red
还是gray
时,它都不是,因为它没有内联样式(style.backgroundColor
实际上是空字符串)。
你有几个选择:
- 使用
getComputedStyle
来查看元素的background-color
实际是什么,无论它是否设置为内联 - 提供一个默认的情况,将设置你的元素的
background-color
,不管它是否已经设置。(如果是红色,切换到灰色;否则设置为红色)
任何一个都可以工作并做你需要的事情;这取决于你的解决方案有多灵活,我把这个留给你。
所以这就是你的if/else应该写的,你不应该在你的else里面有检查。现在,console.log将显示代码失败的原因。在大多数浏览器中,backgroundColor检查将返回rgb,而不是十六进制。
…这不是故意的…
function changeColor(cell) {
var red = '#FE2E2E';
var grey = '#E6E6E6';
console.log(cell.style.backgroundColor); //rgb(230, 230, 230)
if (cell.style.backgroundColor == grey) {
cell.style.backgroundColor = red;
} else {
cell.style.backgroundColor = grey;
}
};
#table tr td {
width: 20px;
height: 50px;
cursor: pointer;
background-color: #E6E6E6;
border: 1px solid black;
}
<table class="table table-bordered" id="table">
<tbody>
<tr>
<td onclick="changeColor(this)"></td>
<td onclick="changeColor(this)"></td>
<td onclick="changeColor(this)"></td>
<td onclick="changeColor(this)"></td>
<td onclick="changeColor(this)"></td>
</tr>
</tbody>
</table>
那么你能做什么呢?使用像RGB到十六进制和十六进制到RGB这样的函数来转换十六进制/RGB,或者你可以使用RGB代替,使它在大多数浏览器RGB(230,230,230)和RGB(254,46,46)中工作,或者代码少得多的简单事情是使用一个类,根本不用担心颜色。
function changeColor(cell) { console.log(cell);
cell.classList.toggle("selected");
};
#table tr td {
width: 20px;
height: 50px;
cursor: pointer;
background-color: #E6E6E6;
border: 1px solid black;
}
#table tr td.selected {
background-color: #FE2E2E;
}
<table class="table table-bordered" id="table">
<tbody>
<tr>
<td onclick="changeColor(this)"></td>
<td onclick="changeColor(this)"></td>
<td onclick="changeColor(this)"></td>
<td onclick="changeColor(this)"></td>
<td onclick="changeColor(this)"></td>
</tr>
</tbody>
</table>
现在2020,我找到了真正的解决方案:
function changeColor(cell) {
var red = "rgba(255, 4, 10, 1)"; //rgba or rgb, so if you really want to use this, you need use regexp.
var grey = "rgba(230, 230, 230, 1)"; //pls change your css to this too.
var style = cell.computedStyleMap().get('background-color').toString();
if (style == grey) {
cell.style.backgroundColor = red;
} else if (style == red) {
cell.style.backgroundColor = grey;
};
对于chrome浏览器这是可以的,但是,也许其他浏览器的其他颜色格式,你需要测试它
这里需要引号。
function changeColor(cell) {
if (cell.style.backgroundColor == "grey") {
cell.style.backgroundColor = "red";
} else if (cell.style.backgroundColor == "red") {
cell.style.backgroundColor = "grey";
};
};
(我还擅自将else块中的if替换为else if)
- jquery.html有效,但是.add不行;t
- 这个JS在我的家用PC上工作,但当我把它放在网上时就不行了.知道为什么或如何这是
- 为什么我的代码使用下划线.js但当我使用 Ramda.js 时却不行
- Ajax请求没有'第二次不行,为什么
- 平滑滚动获胜'不行,为什么
- 如果语句按预期工作,为什么这不行
- 尝试使用 .toUpperCase() 将字母转换为大写.不行
- IE浏览器的Javascript问题,在谷歌浏览器中工作正常,但在IE中则不行
- JavaScript 中的阶乘.不行
- 遍历 xml 在 FF 中工作正常,但在使用 jquery 的 IE 中则不行
- 为什么少不行
- 在本地机器上,我的简单js验证可以工作,但在jsfiddle上却不行;t为什么
- 我的按钮以前可以用,但现在不行了;t
- Javascript返回不'不行?返回undefined
- D3鼠标悬停在本地主机上可以,但在部署站点上不行
- 在JS模板中添加逻辑?好主意还是不行
- 使用REST发送ajax请求是可行的,但使用jQuery则不行
- CSS效果可以很好地处理静态表数据,但当通过javascript添加表数据时就不行了
- 为什么这个jQuery代码在Chrome和Firefox中运行良好,而在IE9中却不行
- 为什么这个动画在IE 11和Edge中工作,但在chrome中不行?