开发者

Efficient way of calling plugin after AJAX call

开发者 https://www.devze.com 2023-02-01 17:07 出处:网络
I am binding elements to a plugin. Then, I am generating more elements through AJAX and rebinding the plugin on callback.

I am binding elements to a plugin. Then, I am generating more elements through AJAX and rebinding the plugin on callback.

Will jQuery rebind the custom plugin for the previous elements? Is there a more efficient solution? Thank you for your help.

$('.class').plugin();

$.post(url, function() {
    // add more .class elements.
    $('.class').plug开发者_StackOverflowin(); // is this efficient?
});


Your plugin should add a class or maybe a .data cache item to elements that have already been activated by the plugin. Then inside your plugin you can ignore an element if it has already been activated.

Also another way is just to select elements from the fragment returned by the ajax call.

$('.class').plugin();

$.post(url, function(response) {
    // add more .class elements.
    $(response).find('.class').plugin(); 
});
0

精彩评论

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