开发者

Why .load is binding event?

开发者 https://www.devze.com 2023-03-09 06:14 出处:网络
I\'m doing this: return this.each(function(){ $.this.load(\'searchInterface.html\',function(){ console.log(\'load\');

I'm doing this:

return this.each(function(){
    $.this.load('searchInterface.html',function(){
        console.log('load');
        //a lot of code
    });
    $('#more_button').bind('click',more());

    function more(){
        console.log('more');
    }
});

And my console is showing:

more

load

That means that somewhere inside .load(), something is clicking on my button. How can I avoid this be开发者_StackOverflow中文版havior?

Thanks!


Declare more above the line that reads $('#more_button').bind('click',more()); and remove the parentheses.

You should be referencing the function, not invoking it.

$('#more_button').bind('click',more);


You are calling the more function. Use:

function more(){
    console.log('more');
}

$('#more_button').bind('click',more);


By including the parentheses, you are calling the more() function rather than passing the reference to id.

Use this instead

$('#more_button').bind('click',more);
0

精彩评论

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