简体   繁体   English

是否可以在ajax调用后重新初始化jQuery的全插件?

[英]is it possible to re-initialize all-plugins for jQuery after ajax call?

If i received ajax response using $.post(...) , $.get(...) or something. 如果我收到使用$ .post(...),$ .get(...)之类的ajax响应。

received response is a html format document and includes jQuery plugins. 收到的响应是html格式的文档,其中包括jQuery插件。

i know jQuery plugin needs to reinitialize after ajax response. 我知道jQuery插件需要在ajax响应后重新初始化。 something like this 像这样的东西

$.get('<ajaxurl>').done(function(response){
    $('#some_element').html(response);
    $('#element1_in_some_element').init_plug_in_1();
    $('#element2_in_some_element').init_plug_in_2();
    $('#element3_in_some_element').init_plug_in_3();
    $('#element4_in_some_element').init_plug_in_4();
    ....
});

But is it possible to re-initialize all plugins after ajax call? 但是有可能在ajax调用之后重新初始化所有插件吗?

like this 像这样

$.get('<ajaxurl>').done(function(response){
    $('#some_element1').html(response);
    $('#some_element1').init_plug_in_all();
});

!!

If you bind to the document, it should allow you to have dynamically added elements on the page which have your plugin automatically applied. 如果绑定到文档,则它应允许您在页面上动态添加自动应用插件的元素。

For example 例如

$('.datePickers').datepicker();

will only fire on document load, and any NEW elements with the "datePickers" class will not have the functionality available. 将仅在文档加载时触发,并且带有“ datePickers”类的任何NEW元素都将不具有可用功能。

But if you attach like this... 但是如果你这样依附...

$(document).on('load','.datePickers', function() {
     $(this).datepicker();
});

It will apply to all elements with the datePickers class, even those added via AJAX 它将应用于具有datePickers类的所有元素,甚至包括通过AJAX添加的元素

If you make a function to initialize the plugins, you can call that function when ajax call ends to reinitialize all your plugins. 如果您创建了一个用于初始化插件的函数,则可以在ajax调用结束时调用该函数以重新初始化所有插件。 There aren't nothing standard to this, all depends on your plugins and your initializators. 这没有什么标准,全部取决于您的插件和初始化程序。 Consider something like this: 考虑这样的事情:

var initializePlugins = function() {
  $('selector').datepicker();
  $('selector2').fullcalendar();
  $('selector3').jScrollPane();
};

// when ajax done
$.ajax().done(function() {
  initializePlugins();
});

// when the DOM is ready        
$(document).ready(function() {
  initializePlugins();
});

If you want $('#some_element1').init_plug_in_all(); 如果要$('#some_element1').init_plug_in_all(); then you could write a plugin. 那么您可以编写一个插件。

$.fn.init_plug_in_all = function(plug_1, plug_2, plug_3, plug_4){

  this.each(function() {

    plug_1 && $(this).init_plug_in_1();
    plug_2 && $(this).init_plug_in_2();
    plug_3 && $(this).init_plug_in_3();
    plug_4 && $(this).init_plug_in_4();

  });
};

then use like:- 然后使用像:

$('#some_element1').init_plug_in_all(true, false, false, true);
$('.some_class').init_plug_in_all(false, false, true, true);
//etc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM