开发者

jquery event bubbling

开发者 https://www.devze.com 2023-03-01 10:29 出处:网络
Can someone explain to me why var divdbl = $(\"div:first\"); var i = 0; divdbl.change(function(){ alert(\'change\');

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

精彩评论

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