jQuery的:阿贾克斯后负荷运行脚本() [英] Jquery: Run script after ajax load()

查看:88
本文介绍了jQuery的:阿贾克斯后负荷运行脚本()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用load()函数,从不同的页面通过点击菜单按钮,动态地获取信息。其中一个按钮指向一个图像库使用它自己的jQuery效果。但是,这些脚本没有运行。我尝试使用getScript加入脚本(),但不工作会明确地加载它们。这里的code:

I am using the load() function to fetch details dynamically from different pages by clicking on buttons in the menu. One of the buttons point to an image-gallery that uses jquery effects of its own. But those scripts are not running. I tried to load them explicitly using getscript() but that doesnt work either. Here's the code :

$(document).ready(function() {

var hash = window.location.hash.substr(1);
var href = $('#nav li a').each(function(){
    var href = $(this).attr('href');
    if((hash==href.substr(0,href.length-4))&&(hash!='')){
        var toLoad = hash+'.php #content';
        $('#content').load(toLoad);
    }                                           
});

$('#nav li a').click(function(){

    var toLoad = $(this).attr('href')+' #content';
    $('#content').hide();   
    window.location.hash = $(this).attr('href').substr(0,$(this).attr('href').length-4);
    $('#content').load(toLoad,showNewContent());
    function showNewContent() {
        $('#content').fadeIn(1000);
        $.getScript("gallery.js");
    }

    return false;

});

});

下面是我的gallery.js文件的内容。确切的效果可能是不相关的,我已经粘贴了以下code,以显示我的提醒盒的位置:

Here's the content of my gallery.js file. The exact effect may be irrelevant, I have pasted below code to show the position of my alert-boxes :

$(document).ready(function(){
//perform actions when DOM is ready
  var z = 0; //for setting the initial z-index's
  var inAnimation = false; //flag for testing if we are in a animation
  alert("1");
  $('#pictures img').each(function() { //set the initial z-index's    
    z++; //at the end we have the highest z-index value stored in the z variable
    $(this).css('z-index', z); //apply increased z-index to <img>
    alert("2");
  });

  function swapFirstLast(isFirst) {
    if(inAnimation) return false; //if already swapping pictures just return
    else inAnimation = true; //set the flag that we process a image
    var processZindex, direction, newZindex, inDeCrease; //change for previous or next image
    alert("3");
    if(isFirst) { processZindex = z; direction = '-'; newZindex = 1; inDeCrease = 1; } //set variables for "next" action
    else { processZindex = 1; direction = ''; newZindex = z; inDeCrease = -1; } //set variables for "previous" action

    $('#pictures img').each(function() { //process each image
    alert("4");
          if($(this).css('z-index') == processZindex) { //if its the image we need to process
        $(this).animate({ 'top' : direction + $(this).height() + 'px' }, 'slow', function() { //animate the img above/under the gallery (assuming all pictures are equal height)
          $(this).css('z-index', newZindex) //set new z-index
            .animate({ 'top' : '0' }, 'slow', function() { //animate the image back to its original position
              inAnimation = false; //reset the flag
            });
        });
      } else { //not the image we need to process, only in/de-crease z-index
        $(this).animate({ 'top' : '0' }, 'slow', function() { //make sure to wait swapping the z-index when image is above/under the gallery
          $(this).css('z-index', parseInt($(this).css('z-index')) + inDeCrease); //in/de-crease the z-index by one
        });
      }
    });

    return false; //don't follow the clicked link
  }

  $('#next1 a').click(function() {
      alert("5");
    return swapFirstLast(true); //swap first image to last position
  });

  $('#prev1 a').click(function() {
      alert("6");
    return swapFirstLast(false); //swap last image to first position
  });

});
//Function for z-index based gallery ends here

下面是我发现:

我做了CTRL + F5和从开始加载页面,并单击库按钮。我得到警告1。该图像加载,但动画不工作。

I do a ctrl+F5 and load the page from start, and click the gallery button. I get Alert "1". The images load, but the animation doesnt work.

我再次点击图库按钮,无需刷新,我从1所有适当的警告为6,效果就像一个魅力。

I click on the gallery button again, without refreshing, and I get all appropriate alerts from 1 to 6 and the effect works like a charm.

在效果不工作,我只是把在Chrome控制台gallery.js的内容,点击进入,而且效果很好再次工作。

When the effect doesnt work, I simply place the content of gallery.js in the chrome-console and click enter, and the effect works perfectly again.

另外,有些时候,效果不重复上述步骤完全相同的工作inspite。可能是什么问题就在这里?听说getScript加入方法是异步,我甚至尝试设置阿贾克斯同步关闭,但我仍然可以取消predictable结果。

Also, there are times when the effects dont work inspite of repeating the above steps exactly. What could be the problem here? I heard the getscript method is async and I even tried setting ajax sync off but I still get unpredictable results.

推荐答案

我发现,尽管调用 getScript加入脚本()作为一个回调的负载(),该脚本加载的之前的内容可能这是搞乱了流量。而不是调用的 showNewContent()作为一个回调,我需要调用 showNewContent 即函数指针(无圆括号),它等待加载完成。继code解决它:

I found out that despite calling getScript() as a callback to load(), the script loading 'before' the content could which was messing up the flow. Instead of calling showNewContent() as a callback I needed to call showNewContent i.e. a pointer to the function(without round brackets), for it to wait for load to complete. Following code resolved it :

$('#content').load(toLoad,showNewContent);
    function showNewContent() {
        $('#content').fadeIn(1000);
        $.getScript("gallery.js");
}

这篇关于jQuery的:阿贾克斯后负荷运行脚本()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆