Firefox在比较操作中未检测到rgb颜色



我用HTML、CSS和&javascript。游戏在Chrome中功能齐全,但我在使用Firefox浏览器时会有一些有趣的行为。

在Firefox中,当玩家掉落棋子时,游戏板的颜色会成功改变,然而,当我在检查玩家是否获胜时进行比较操作时,Firefox不会检测到任何颜色。

我试图通过声明一个特定的rgb值来解决这个问题,而不是只说"紫色",我做了下面这样的操作,得到了类似的结果。

以下是我的javascript代码的相关部分:

const _purple = "rgb(124, 0, 132)";
columns[id][5 - pcs[id]].style.background = _purple;  // assignment works always
columns[id][5 - pcs[id]].style.background = 'green';  // assignment like this ok too
if (columns[_col][_row].style.background == _purple)  // only works in Chrome
if (columns[_col][_row].style.background == 'green') // only works in Chrome

我还试着按照一个朋友的建议使用===

*****更新*****我检查了控制台,查看Firefox返回了第一条评论中建议的内容,Firefox似乎返回了正确的值。

rgb(124, 0, 132) none repeat scroll 0% 0% 
green none repeat scroll 0% 0% 

但是比较操作永远不会返回true。我试图只使用整个字符串,但也不起作用。

不出所料,在Chrome上,如果我只是使用"rgb(124, 0, 132)"而不是_purple它也返回true。

如果它对解决这个问题有任何帮助我在中使用的CSS元素游戏板是:

.box {
background: white;
border: 2px solid black;
}

进一步:

视觉工作室向我报告说;类型字符串"上不存在属性颜色;。对console.log(first.style.background.color);的调用在Firefox控制台中返回undefined,而对console.log(second.style.background);的调用返回正确的rgb值。当比较未定义的.color值时,它返回true,但当比较.background属性时,它会返回false,该属性实际上是rgb值

感谢您对此问题的帮助,可以在此处查看游戏和更正此错误的尝试https://github.com/paolo-Rancati/four_in_a_sequence/tree/main/snippet

您可以使用computedStyle解决此问题,显然您拥有的"rgb(124, 0, 132)"的rgb值与FireFox用于purple的值不对应。

const _purple = "rgb(128, 0, 128)";
let first = document.getElementById("first");
let second = document.getElementById("second");
const spaces = document.querySelectorAll('.box');
first.style.background = "purple";
second.style.background = _purple;
console.log(window.getComputedStyle(first).backgroundColor);
console.log(window.getComputedStyle(second).backgroundColor);
console.log(window.getComputedStyle(first).backgroundColor === window.getComputedStyle(second).backgroundColor);
div {
width: 100%;
height: 50px;
border: 1px solid black;
}
.box {
background: white;
border: 2px solid black;
}
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"></html>
<head>
<link rel="stylesheet" href="static/website.css">
<meta charset="utf-8" />
<title>Game Page</title>
</head>

<div id="first"></div>
<div id="second"></div>

</html>

但是为什么要测试单元格的颜色??相反,您应该有一个存储板状态的数组,并测试数组中的值!我知道这不能回答你的问题,但你有一个XY问题。

最新更新