Jquery:在ajax load()之后运行脚本

标签 jquery ajax

我正在使用 load() 函数通过单击菜单中的按钮从不同页面动态获取详细信息。其中一个按钮指向一个使用自己的 jquery 效果的图像库。但这些脚本没有运行。我尝试使用 getscript() 显式加载它们,但这也不起作用。代码如下:

$(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 文件的内容。确切的效果可能无关紧要,我粘贴了下面的代码来显示我的警报框的位置:

$(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”。图像已加载,但动画不起作用。

我再次单击图库按钮,无需刷新,我收到了从 1 到 6 的所有相应警报,效果非常神奇。

当效果不起作用时,我只需将 gallery.js 的内容放入 chrome-console 中并单击 Enter,效果就会再次完美运行。

此外,有时即使完全重复上述步骤,效果也不起作用。这里可能有什么问题?我听说 getscript 方法是异步的,我什至尝试将 ajax 同步设置为关闭,但我仍然得到不可预测的结果。

最佳答案

我发现,尽管调用 getScript() 作为对 load() 的回调,加载内容的脚本“之前”这扰乱了流程。我不需要调用 showNewContent() 作为回调,而是需要调用 showNewContent,即指向函数的指针(不带圆括号),以便它等待加载完成。以下代码解决了这个问题:

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

关于Jquery:在ajax load()之后运行脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12773600/

相关文章:

javascript - Jquery/PHP : Javascript function fails to run

jquery - AJAX 调用期间 Node.js 响应问题

javascript - Json和html显示

php - 将数据保存到 MySQL 并使用该数据更新页面而无需刷新

jquery - 如何在 jQuery 中为 getJSON 设置缓存 false?

javascript - Tiny_mce - 无法设置宽度

jquery - 使用 jQuery 将函数惰性绑定(bind)到多个事件

javascript - Jquery-te如何停止javascript执行?

php - 如何在 laravel 中使用 ajax 验证输入数据

jquery - 房间:362 Uncaught TypeError: Cannot read property 'length' of undefined