binding events in jquery to replaced html
I'm building a jquery plugin for use in my projects in editing items in a table. When a user clicks on an item to edit, I do a pop-up form where they then save the data. When the save is successful I return the new data as a html row and replace the old row with this new returned row, but then I have to rebind the events. I can't figure out how to do this in开发者_运维知识库 a generic way in jquery. I am not sure if using .live() in a plug in is also good practice or if it will always work.
Currently I'm calling a call back function to rebind the events which then needs to happen on each page where I want to use it, I would like the plug in to do this for me instead.
Jquery live() will work in your situation.
it's detailed documentation can be found out below -
http://api.jquery.com/live/
Using jQuery, you won't have any other choice than using the live or delegate function. Any new DOM element won't be recognize otherwise.
As you can see http://api.jquery.com/category/events/event-handler-attachment/, they both
Attach a handler to the event for all elements which match the current selector, now and in the future.
精彩评论