我正在尝试这样的事情:
var usernameDone = false;
/*Some application logic*/
alert(usernameDone);
Run Code Online (Sandbox Code Playgroud)
这总是在评估undefined
.但是,如果我将变量名称更改usernameDone
为其他任何名称,即使usrnameDone
脚本也能正常工作.是否usernameDone
冲突与jQuery?如果没有,那为什么会发生?
(我目前正在使用jQuery-1.6.1在Chrome上进行测试,但尚未尝试其他浏览器.)
[更新]即使消除了应用程序逻辑
,变量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);
}
});
});
Run Code Online (Sandbox Code Playgroud)
当然会是这样undefined
,因为你ready
在字符串'document'
上监听不在变量上的事件document
.不要document
用单引号或双引号括起变量,否则ready
事件永远不会触发,因此永远不会定义变量.另外,不要再声明变量usernameDone
有var usernameDone = true;
,只是分配这样一个值给它usernameDone = true;
,因为它已被定义.
归档时间: |
|
查看次数: |
3704 次 |
最近记录: |