jquery sliders not working when loading through ajax

落花浮王杯 提交于 2019-12-12 06:23:09

问题


I am using the jquery slider plugin to create some sliders. This works fine when I load them directly when the dom is initiated however when I try to load the sliders into a div using the ajax innerHTML method they do not get intiated.

Does anything know how I can do this?


回答1:


You have to initiate / bind your events again.

e.g

//Ajax done and innerHTML is set
$('container').slider(); //Just an example... this will bind events.

Or

You can change the event handlers to .live . This handles dynamic content.




回答2:


Sliders are not being initialized when creating them by settings the innerHTML of an object. Try this instead (for a range slider):

$.ajax({
  url: "get_slider.php",
  success: function(data){
    if(typeof(data) == 'string') data = JSON.parse(data);
    $('#my_slider_id').slider({
            range: true,
            min: data.min,
            max: data.max
        });
  },
  error: function(data){
    // Something went wrong, do stuff here
  }
});

Here we expect get_slider.php to return a JSON object containing at least a min and a max property for instantiating the slider.




回答3:


When u are adding sliders to div using innerHTMl, you have to again call the sliders setup methods so that the events are attached to the sliders when the sliders are added to div. Thanks Ashwani




回答4:


Try use the OnComplete property within the $.ajax method to trigger the method for the sliders. That will ensure that the method will only be triggered once the ajax content has been loaded.



来源:https://stackoverflow.com/questions/6829879/jquery-sliders-not-working-when-loading-through-ajax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!