我在學校爲班級創建了一個記憶遊戲,我使用的是Bootstrap和jQuery。見Github。爲了測試,使用這個jsfiddle,因爲github代碼會改變,如果你想爲自己的目的分叉它,我已經包含了它。jQuery第二次點擊後解除綁定事件
我已經修建了下面的邏輯代碼:
- 你想要多少牌可打挑選。
- 卡片加載和隨機化。每對具有相同的類別(卡片*和圖形*)。
- 你點擊一張卡片,然後在另一張卡片上,如果他們匹配,他們會被丟棄,否則你會再次選擇。
我目前遇到的問題是第三步,即當你點擊第三張顯示前兩張時,這意味着我需要包括一些東西來逃避第一次點擊事件。至少這是我對這個問題的第一個建議。如果你有其他建議來完全重組第三步,請不要害羞地說明原因。
// check if picked cards' classes match
jQuery("[class^=card]").click(function() { //picking the first card
jQuery(this).css('color', '#000');
var firstCard = $(this);
var firstCardClass = $(this).find('[class*=glyphicon]').attr('class').split(' ')[1];
jQuery("[class^=card]").click(function() { //picking the second card
var secondCard = $(this);
var secondCardClass = $(this).find('[class*=glyphicon]').attr('class').split(' ')[1];
console.log(firstCardClass);
console.log(secondCardClass);
if (firstCardClass == secondCardClass) {
console.log("yes")
$(firstCard).css('color', '#005d00'); //make them green
$(secondCard).css('color', '#005d00');
setTimeout(function() {
$(firstCard).css('display', 'none'); //discard
$(secondCard).css('display', 'none');
}, 1000);
}
else {
console.log("no");
$(firstCard).css('color', '#cc0000'); //make them red
$(secondCard).css('color', '#cc0000');
setTimeout(function() {
$(firstCard).css('color', '#fff'); //hide again
$(secondCard).css('color', '#fff');
}, 1000);
}
});
});
注意的圖標應該是白色的卡片,讓他們灰色的,看那些女巫匹配,而不需要螢火蟲的。如果你點擊多於兩張卡,你會看到問題所在(如果我沒有解釋清楚)。我嘗試在每個語句的末尾添加單擊解除綁定事件,但無法使其工作。
盡力而爲!謝謝!
只是嘗試$(EL).off (「事件名稱」)後,第二次點擊..將需要有一個計數器,雖然 –
我已經嘗試過關閉()和解除綁定(),但我想卡後點擊,以便該過程可以重新開始開始。我怎麼做? – pres
你不需要'click'裏面的'click',因爲它們都綁定到同一組選擇器。 – lshettyl