开发者

JQuery focus/blur events help

$(document).ready(function() {

    $('#username').focus(function() {
        $(this).val('');
 开发者_JS百科   })
    $('#username').blur(function() {
        var l = $(this).val.length;
        if (l == 0) {
            $(this).val('Username');
        }
    })

});

What the above code is supposed to do is empty the value of #username input field when focused and fill in the user friendly "Username" there when it loses focus in case it's still empty.

The second part doesn't work though (I think there is some problem with the if condition I'm using?).


For the second line of your blur function try

var l = $(this).val().length;

Also, try alerting this value to see if it is finding it okay.


Use $(this).val().length instead. $(this).val only references the function. And the length attribute of a function is the number of expected arguments and not the length of the return value of that function.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜