1

我有以下 javascript。例如console.log('test')加载 javascript 时的触发器。我希望它在我单击带有类登录的按钮时触发。我究竟做错了什么?

$(document).ready(function() {
    switchImage(5000, '#top .images');
    switchImage(5000, '#top .feature-text');
    $('.login input').focus(function(){
        $('.login .hidden').removeClass("hidden");
        $('.login [type=text]').removeClass("span2").addClass("span3");
    });
    $('.loginbutton').click(login());
});


function login(){
    event.preventDefault();
    console.log('test')
}

function switchImage(wait, element){

    //Fades out first element and put's it in the bottom of the stack
    $(element+'>*:first-child').delay(wait).fadeOut('slow', function() {

        //fade in imag2
        $(element+'>*:nth-child(2)').fadeIn('slow');
        $(element+'>*:first-child').appendTo(element); 
        switchImage(wait, element);
    });
}
4

1 回答 1

8

你有:

$('.loginbutton').click(login());

这表示“单击按钮时,调用login().

你想要的是:

$('.loginbutton').click(login);

即“单击按钮时,调用login函数”。

于 2012-10-05T21:29:49.147 回答