HTML/CSS 代码与问题无关,大多数 JavaScript 代码也是如此
在line 51
JavaScript 中,console.log(mode);
我期待看到1
或2
但我得到undefined
. 我唯一的猜测是该值是在内部代码$pregame.on('click', 'button', function () { ...
完成其工作之前记录的。
如何在// ------------ game code ------------
上面的代码之后运行代码?我有什么选择?
jQuery 代码
(function($) {
var $pregame = $('div#pregame');
var $notifier = $('div#notifier')
var $game = $('div#game');
var mode;
for (var i = 0; i < 8; i++) {
$game.append($('div.frame').eq(0).clone());
}
var $frame = $game.find('div.frame');
$(window).on('resize', function() {
var windowW = window.innerWidth, windowH = window.innerHeight;
var gameLen = windowW < windowH * 0.9 ? windowW : windowH * .9;
$game.css({
'width': gameLen + 'px',
'height': gameLen + 'px'
});
}).trigger('resize');
$pregame.on('click', 'button', function () {
if ( $(this).data('mode') === 1 ) {
mode = 1;
} else {
var $divs = $notifier.children('div');
$divs.eq(0).text('Player 1').end().eq(3).text('Player 2');
mode = 2;
}
$pregame.fadeOut(500, function () {
$game.fadeIn(500);
$notifier.css('display', 'table').animate({'opacity': 1}, 500);
});
$pregame.off('click');
});
// ------------ game code ------------
var activeFrame = 4, gameArray = [];
for (var i = 0; i < 9; i++) {
gameArray.push(['-','-','-','-','-','-','-','-','-']);
}
console.log(mode); // <<----- THIS HERE <<---------
if ( mode === 1 ) {
$('body').on('click', 'div.frame', function() {
var index = $frame.index(this);
if ( index === activeFrame ) console.log('click on active Frame');
});
} else if ( mode === 2 ) {
}
})(jQuery);