2012-10-05 67 views
1

我有以下的JavaScript。例如,当我加载JavaScript时,触发器会触发console.log('test')。我希望当我点击一个带有类登录的按钮时它会被触发。我究竟做错了什么?为什么我的函数在页面加载时触发,我该怎么办?

$(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); 
    }); 
} 

回答

8

您有:

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

这是说“当按钮被点击时,调用的login()结果

你想要的是:

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

哪个“点击该按钮后,调用login函数”。

相关问题