Ajax Dynamic Refresh element js event failed, ajax dynamic element js
When I encountered a situation in my work, after dynamically refreshing a region through Ajax, The js events originally bound to these dom elements became invalid and I did not know the reason for locating them for a long time, later, I checked the information and found that the region Dom element after dynamic refreshing is re-generated, so the js elements before the page are not dynamically bound to the newly generated element. How can this problem be solved, the event Delegate mechanism of jQuery is used here. The sample code is as follows:
Before jQuery1.7, you can use the live event delegation method:
$ (Selector). live ("click", function () {// Event Logic });
JQuery abolished the live event after version 1.7, but adopted a unified api method, which is good news for program developers and does not need to remember so many APIs, unified on call:
$ (Document). on ("click", selector, function () {// Event Logic })
We recommend that you use the on Method in the new version. document tries its best to bind the smallest element of the event, such as the parent element. This will provide better performance. selector is the selector to bind the element.