Jquery在控制台中运行良好,但在我加载页面时却没有

时间:2012-10-06 22:47:08

标签: jquery

我有一个用于图像库的jquery代码,我在js文件中还有一些其他函数。当我通过单击链接加载图库时,图像会出现,但效果不起作用。但是,当我将相关代码粘贴到chrome控制台并按回车键时,它就像一个魅力。我忽略了某种冲突吗? 我的jquery代码如下(尽管我认为它没有问题,因为当我在单独的网页或控制台上独立运行代码时它运行良好):

$(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

  $('#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>
  });

  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

    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
      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
  }

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

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

更新1 : 脚本中有另一个hashchange函数,它是这样的:

$(function(){
  $(window).hashchange( function(){
    $(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').load(toLoad);

});

});
  })

似乎每次在URL中都有hashchange时调用此函数,这可能是我的gallery-function未被调用的原因。我已经尝试将gallery函数放在hashchange函数中,但它不起作用。我觉得这里存在冲突,因为我的图库功能中的警报没有触发。

更新2:我在jquery-documentation中读取了load函数,如果执行load函数就像这样$('#b')。load('article.html #target');然后不执行正在加载的文档中的脚本块。这可能是问题,我该如何克服这个问题?

0 个答案:

没有答案