开发者

Does .live() need a selector to start with?

I am using jquery as so

$('.mybutton').live('click', function(){
   // do something
});

this is called when the document is ready but when the document is ready there are 'mybutton' classes being used, but if the user clicks somewhere a new form appears which uses the button with the class 'mybutton'. But this does not seem to be working, and it doesn't have the required handler.

Is this because there where no 'mybutton' classes to s开发者_如何学JAVAtart with on the document ready?


If live() ever doesn't seem to work for an element that has the proper selector, like .mybutton in your case, it is likely because some ancestor element of the .mybutton is preventing bubbling from happening.

If any ancestor of a .mybutton has:

return false;

or:

event.stopPropagation();

This will effectively disable .live() for that .mybutton because .live() needs the event to bubble all the way up to the root.


Perhaps you could try this instead:

$('#myForm').delegate('.mybutton', 'click', function() {
   // do something.
})


EDIT: okay, i miunderstood your question.
This is more an answer to "Is a selector always required with live()?".


Yes.
See the caveats section on this wiki page.

The reason why a selector is required with jquery.live() is explained here.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜