我现在完全迷路了。我上传了我的整个代码。有人可以帮助我吗,我将非常感激!如何将其更改为不使用 jQuery,只使用 javaScript?
var CARDS_NUM = 20; // number of cards in total
var ROW = 5; //number of cards in rows
var cards = [];
var takenCards = [];
var availableCards= true; //cards which are available to choose
var attemptsNumber = 0; //number of moves
var pairs= 0; //number of guessed pairs of cards
var cadsImages = [
'duck.PNG',
'elephant.png',
'monkey.png',
'pig.png',
'pinguin.png',
'cow.png',
'sheep.png',
'zebra.png',
'lion.png',
'horse.png'
];
function startGame() {
cards = [];
takenCards = [];
availableCards = true;
attemptsNumber = 0;
pairs = 0;
var board = $('.board').empty();
for (var i=0; i<CARDS_NUM; i++) {
cards.push(Math.floor(i/2));
}
您建议我有一部分:
for (i=CARDS_NUM-1; i>0; i--) { var swap = Math.floor(Math.random()*i); var tmp = 卡片[i]; 卡[i] = 卡[交换]; 卡[交换] = tmp; }
for (var i = 0; i < CARDS_NUM; i++) {
var tile = document.createElement('card');
tile.className = 'card';
tile.setAttribute('data-cardType', cards[i]);
tile.setAttribute('data-index', i);
var current_width = tile.style.width,
current_height = tile.style.height;
tile.style.left = 5 + (current_width + 5) * (i % ROW);
tile.style.top = 5 + (current_height + 5) * (Math.floor(i / ROW));
board.appendChild(tile);
}
document.addEventListener('click', function(event) {
var target = event.target || event.srcElement;
cardClick(target);
});
结束它。
function cardClick(element) {
if (availableCards) {
//if none element is taken
//or if element's index is not in taken cards
if (!takenCards[0] || (takenCards[0].data('index') != element.data('index'))) {
takenCards.push(element);
element.css({'background-image' : 'url('+cadsImages[element.data('cardType')]+')'})
}
if (takenCards.length == 2) {
takenCards = false;
if (takenCards[0].data('cardType') == takenCards[1].data('cardType')) {
setTimeout('deleteCards()', 500);
} else {
setTimeout('resetCards()', 500);
}
attemptsNumber++;
$('.cards').html(attemptsNumber)
}
}
}
function deleteCards() {
takenCards[0].fadeOut(function() {
$(this).remove();
});
takenCards[1].fadeOut(function() {
$(this).remove();
pairs++;
if (pairs >= CARDS_NUM / 2) {
alert('gameOver!');
}
availableCards = true;
takenCards = new Array();
});
}
function resetCards() {
takenCards[0].css({'background-image':'url(title.png)'})
takenCards[1].css({'background-image':'url(title.png)'})
takenCards = new Array();
availableCards = true;
}
$(document).ready(function() {
$('.start_game').click(function() {
startGame();
});
})