开发者

jquery event bubbling

Can someone explain to me why

var divdbl = $("div:first");
var i = 0;

divdbl.change(function(){
    alert('change');    
}).click();

divdbl.click(function(){
    i++;
    alert('click '+i);
}).change();

behaves like they are calling themselves, even if I do it like this

divdbl.click(function(){
    i++;
    alert('click '+i);
}).change();

div开发者_Go百科dbl.change(function(){
    alert('change');    
}).click();

its as if they are calling themselves instead of the other.

EDIT

var divdbl = $("div:first");
var i = 0;

divdbl.change(function(){
    alert('change');    
});

divdbl.click(function(){
    i++;
    alert('click '+i);
}).change().click();

i just realized that what i wanted to accomplish was to force them to run once on load Would the above code make sense?


Well, first of all, there's an issue because you are calling triggering change() handler before it is defined. What are you trying to accomplish? Calling .change() and .click() runs the functions for those events.

EDIT:

Based on your recent update to your question: Yes, that will work as intended. Both .click and .change will be invoked upon load.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜