[英]if img.src === img.src === img.src - cant get it to work
I am trying to make a Match3 like Game with images, but I cant get the comparison to work. 我正在尝试使用图像制作像Match3这样的游戏,但无法进行比较。 I am doing this for a fixed number atm, just for getting it to work, should be in a foreach loop later.
我这样做是为了固定数量的atm,只是为了使其正常工作,以后应该在foreach循环中。 The function is inside a "DOMContentLoaded" Listener if that makes any difference.
该函数位于“ DOMContentLoaded”侦听器内部(如果有任何区别)。 This: if statement with img.src and this: Getting img.src value (path name) did not help me and I am not able to find or see a solution.
这: 带有img.src的if语句,以及以下内容: 获取img.src值(路径名)并没有帮助我,也无法找到或看到解决方案。 Thanks for any help or suggestions!
感谢您的帮助或建议!
function removeTiles() {
tiles = document.getElementsByTagName("IMG");
/*for ( var j = 0; j < tiles.length; ++j) {*/
var x = 15;
var y = x-1;
var z = x+1;
/*I tried it with tiles[x].src before, gave me the same console and alert output, but the other post suggested trying this, so I did */
var x2 = tiles[x].getAttribute('src', 2);
var y2 = tiles[y].getAttribute('src', 2);
var z2 = tiles[z].getAttribute('src', 2);
alert(x2);
alert(y2);
alert(z2);
if (x2 === y2 === z2){
tiles[z].parentNode.removeChild(tiles[z]);
tiles[y].parentNode.removeChild(tiles[y]);
tiles[x].parentNode.removeChild(tiles[x]);
} else {
console.log('whatever');
console.log(x2);
console.log(y2);
console.log(z2);
}
console.log(tiles[x]);
}
/*}*/
window.onload = removeTiles();
The console output is: 控制台输出为:
whatever
coffee.gif
coffee.gif
coffee.gif
<img src="coffee.gif">
you cannot chain them like x2 === y2 === z2
the first part will evaluate to a boolean (ie true
or false
) which compared via ===
to a string will always yield false
. 您不能像
x2 === y2 === z2
那样链接它们,第一部分将评估为布尔值(即true
或false
),该布尔值通过===
与字符串进行比较将始终产生false
。
You will need to do it like x2 === y2 && y2 === z2
您将需要像
x2 === y2 && y2 === z2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.