我想是這樣的:jQuery的變量總是爲「未定義」
var usernameDone = false;
/*Some application logic*/
alert(usernameDone);
這是總爲undefined
。但是,如果我將變量名稱從usernameDone
更改爲其他任何內容,即使usrnameDone
,該腳本也能正常工作。 usernameDone
與jQuery有衝突嗎?如果不是,那麼爲什麼會發生?
(我測試在Chrome與jQuery-1.6.1和目前還沒有嘗試過其他瀏覽器。)
[更新]
變量的計算結果爲undefined
即使應用程序邏輯被消除。
[更新-2的完整代碼]
我已標記使用//
評論
$('document').ready(function() {
/**
* The following set manages the effects for the login menu. The effects including highlighting
* the login box when focused, showing guidance through a pop-up menu and handling transformation
* through fade effects.
*
*/
var usernameDone = false; //Declared here
/**
* @function login-box focus()
* Handles the onFocus() effects of the login box and displays the initial guidance pop-up box.
*
*/
$('#login-box').focus(function() {
/**
* Color change effect of the login box
*/
$(this).css('background-color' , '#000');
$(this).css('color' , '#FFF');
$(this).css('border' , '2px solid #000');
/**
* Fade in effect of the login guide
*/
$('#login-guide').fadeIn(1200);
$('#login-guide-triangle').fadeIn(1200);
});
/**
* @function login-box focusout()
* Handles the focusOut effects of the login box and hides the guidance pop-up box.
*
*/
$('#login-box').focusout(function() {
/**
* Color change effects of the login box
*/
$(this).css('background-color' , '#FFF');
$(this).css('color' , '#000');
$(this).css('border' , '3px solid #000');
/**
* Fade out effect of the login guide
*/
$('#login-guide').fadeOut(1200);
$('#login-guide-triangle').fadeOut(1200);
});
/**
* @function login-box bind(keypress)
* Handles the progression of the login steps. Specifically, what state the login box is in
* (ie, username or password) and how to respond when the state change key ('return/enter key')
* is pressed.
*
*/
$('#login-box').bind('keypress' , function(e) {
/**
* Checks for the current state (using usernameDone) if not, then registers the current username
* changes the state to username done and converts the login box into password. Also, changes the
* guidance contents.
*/
alert(usernameDone); //Always undefined (no modification before this)
if(e.keyCode == 13 && usernameDone == false) { //Owing to undefined it never evaluates to true.
var loginUsername = $(this).val();
$(this).val('');
var usernameDone = true;
$(this).fadeOut(0);
document.getElementById('login-box').setAttribute('type' , 'password');
$('#login-guide-title').html('Password');
$('#login-guide-info').html('Press enter to login');
$(this).fadeIn(800);
}
});
});
我認爲,我們需要看到「一些應用程序邏輯」的內容,以瞭解您的問題:) –
剛試過它在Chrome的JavaScript控制檯,工作正常。 – Giann
無法重現:http://jsfiddle.net/fkling/fkq99/(使用Chrome和jQuery)。你肯定是在某處做某事的;) –