[英]if img.src === img.src === img.src - cant get it to work
我正在嘗試使用圖像制作像Match3這樣的游戲,但無法進行比較。 我這樣做是為了固定數量的atm,只是為了使其正常工作,以后應該在foreach循環中。 該函數位於“ DOMContentLoaded”偵聽器內部(如果有任何區別)。 這: 帶有img.src的if語句,以及以下內容: 獲取img.src值(路徑名)並沒有幫助我,也無法找到或看到解決方案。 感謝您的幫助或建議!
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();
控制台輸出為:
whatever
coffee.gif
coffee.gif
coffee.gif
<img src="coffee.gif">
您不能像x2 === y2 === z2
那樣鏈接它們,第一部分將評估為布爾值(即true
或false
),該布爾值通過===
與字符串進行比較將始終產生false
。
您將需要像x2 === y2 && y2 === z2
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.