最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - jQuery scrollbar plugin not working on Ajax loaded content - Stack Overflow

programmeradmin2浏览0评论

The problem is this:

I have a simple, two fields form which I submit with Ajax. Upon pletion I reload two div's to reflect the changes.

Everything is working perfect except a jQuery plugin. It's a simple plugin that can be called with simple

function(){
    $('.myDiv').scrollbars();
}

It's simple and easy to use, but it doesn't work on Ajax loaded content. Here is the code I use to post form and reload div's:

$(function() {
    $('#fotoent').on('submit', function(e) {
        $.post('submitfotoent.php', $(this).serialize(), function (data) {
            $("ent").load("fotoajax.php");
        }).error(function() {

        });
        e.preventDefault();
    });
});

I've tried creating a function and calling it in Ajax succes:, but no luck. Can anyone show me how to make it work ? How can that simple plugin can be reloaded or reinitialized or, maybe, refreshed. I've studied a lot of jQuery's functions, including ajaxStop, ajaxComplete ... nothing seems to be working or I'm doing something wrong here.

The problem is this:

I have a simple, two fields form which I submit with Ajax. Upon pletion I reload two div's to reflect the changes.

Everything is working perfect except a jQuery plugin. It's a simple plugin that can be called with simple

function(){
    $('.myDiv').scrollbars();
}

It's simple and easy to use, but it doesn't work on Ajax loaded content. Here is the code I use to post form and reload div's:

$(function() {
    $('#fotoent').on('submit', function(e) {
        $.post('submitfotoent.php', $(this).serialize(), function (data) {
            $(".ent").load("fotoajax.php");
        }).error(function() {

        });
        e.preventDefault();
    });
});

I've tried creating a function and calling it in Ajax succes:, but no luck. Can anyone show me how to make it work ? How can that simple plugin can be reloaded or reinitialized or, maybe, refreshed. I've studied a lot of jQuery's functions, including ajaxStop, ajaxComplete ... nothing seems to be working or I'm doing something wrong here.

Share Improve this question edited Aug 16, 2013 at 17:50 Sasidhar Vanga 3,4042 gold badges28 silver badges47 bronze badges asked Aug 13, 2013 at 12:59 Lucian MineaLucian Minea 1,33410 silver badges13 bronze badges 2
  • have you added jquery.scrollbars.css file on your web page correctly? – vijayP Commented Aug 13, 2013 at 13:05
  • You're not getting the point.Plugin is working smoothly but not when I reload the content with Ajax. – Lucian Minea Commented Aug 13, 2013 at 16:27
Add a ment  | 

4 Answers 4

Reset to default 2

If you're loading elements dynamically after DOM Document is already loaded (like through AJAX in your case) simple binding .scrollbars() to element won't work, even in $(document).ready() - you need to use "live" event(s) - that way jQuery will "catch" dynamically added content:

$(selector).live(events, data, handler); // jQuery 1.3+
$(document).delegate(selector, events, data, handler); // jQuery 1.4.3+ 
$(document).on(events, selector, data, handler); // jQuery 1.7+

Source: jQuery Site

Even if I am totally against using such plugins, which tries to replicate your browser's ponents, I'll try to give some hints.

I suppose you are using this scrollbars plugin. In this case you may want to reinitialize the scrollbars element, and there are many ways to do this. You could create the element again like in the following example

<div class="holder">
  <div class="scrollme">
    <img src="http://placekitten./g/400/300" />
  </div>
</div>

.....

$('.scrollme').scrollbars();

...

fakedata = "<div class='scrollme'>Fake response from your server<br /><img src='http://placekitten./g/500/300' /></div>";
$.post('/echo/html/', function(response){
    $('.holder').html(fakedata);
    $('.scrollme').scrollbars();
});

If you want to update the contents of an already initialized widget instead, then things gets more plicated. Once your plugin initialize, it moves the content in some custom wrappers in order to do its 'magic', so make sure you update the correct element, then trigger the resize event on window, pray and hopefully your widget gets re-evaluated.

If it doesn't help, then try to e up with some more details about your HTML structure.

I want to thank everyone of you who took their time to answer me with this problem I have. However, the answer came to me after 4 days of struggle and "inventions" :), and it's not a JS or Jquery solution, but a simple logic in the file.

Originally, I call my functions and plugins at the beginning of the document in "head" tag, like any other programmer out here (there are exceptions also ).

Then my visitors open my blog read it and they want to post ments. But there are a lot of ments, and I don't want to scroll the entire page, or use the default scroll bars, simply because they're ugly and we don't have cross browser support to style that, just yet.

So I .post() the form with the ment, and simply reload the containing all of them. Naturally .scrollbars() plugin doesn't work. Here e the solution.

If I put this :

<script>$('.showent').scrollbars();</script>

in the beginning of my loaded document (with load() ), will not work, because is not HTML and it's getting removed automatically. BUT !!! If i do this:

<div><script>$('.showent').scrollbars();</script></div>

at the same beginning of loaded document, MAGIC .... it works. The logic that got me there I found it in the basics of javascript. If your script is inside an HTML element, it will be parsed without any problem.

Thank you all again, and I hope my experience will help others.

If I understand you correctly, try this:

var scrollelement = $('.myDiv').scrollbars();
var api = scrollelement.data('jsp');

$(function () {
    $('#fotoent').on('submit', function (e) {
        $.post('submitfotoent.php', $(this).serialize(), function (data) {
            $(".ent").load("fotoajax.php");
            api.reinitialise();
        }).error(function () {

        });
        e.preventDefault();
    });
});

reinitialise - standart api function, updates scrolbars.

发布评论

评论列表(0)

  1. 暂无评论