我有一个容纳所有白棋子的物体和一个容纳所有黑棋子的物体。现在我正在编写一个 if 语句来检查我使用的部分是否具有相同的颜色。
对象的代码是:
var whiteFgrs = {King:"♔", Queen:"♕", Fortress:"♖", Bishop:"♗", Knight:"♘", Peasant:"♙"};
var blackFgrs = {King:"♚", Queen:"♛", Fortress:"♜", Bishop:"♝", Knight:"♞", Peasant:"♟"};
以及存储所选位置值的代码:
var value1=document.getElementById(elemId1).lastChild.nodeValue;
var value2=document.getElementById(element.id).lastChild.nodeValue;
这将返回该片段(例如“♙”)。如何检查此代码是否是whiteFgrs或blackFgrs的一部分?
最佳答案
嗯,您可以像其他人建议的那样在对象中查找它,但是有一种更简单的方法来确定一 block 是黑色还是白色:
value < "♚" ? "white" : "black";
// or rather, as `nodeValue` gives you the plain text without html entities:
value < "\u265a" ? "white" : "black";
关于javascript - 通过对象的属性之一查找对象的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27411081/