2010-10-22 73 views
0

我想找出如何让一个jQuery脚本正常工作,当点击事件开始时,脚本计算一个容器的高度(这个脚本出现在加载页面的开始并且工作100%),但是我现在试图让它在.click事件中工作。jQuery脚本在DOM在.click事件中加载后工作?

如果有人知道我应该如何实现这一点,我将非常感激。

p.s.我已经尝试做好文件准备;等待DOM加载,但没有雪茄。

$(document).ready(function() { 
    $("#hidden4").click(function(event) { 
     event.preventDefault(); 
     $("#hidden4-1").slideToggle(); 

     var classDown = $('#hidden4 div.down').attr('class'); 
     var classUp = $('#hidden4 div.up').attr('class'); 

     if(classDown == 'down') { 
      $("#hidden4 div.down").attr("class","up"); 
     } 
     else if(classUp == 'up') { 
      $("#hidden4 div.up").attr("class","down"); 
     } 
      // Attain the absolute height of the container id container and assign to a usable variable 
      var height = $("#container").height(); 
      alert (height); 
      // Use the previous variable, divide by 4 then round that up and multiply by 4 and assign to new variable 
      var newHeight = Math.ceil(height/4) * 4; 

      // Create a new variable and add the string "center" to it 
      var finalHeight = "center "; 

      // Using the previous variable add to it using the first 2 variables subtracting to find the difference and add 2 
      finalHeight += (newHeight - height)+2; 

      // Using the previous variable add to it the string "px" for the css selector usage 
      finalHeight += "px"; 

      // Update the CSS of the required element altering the background position with the final variable 
      $(".contentFooter").css('background-position', finalHeight); 
    }); 
}); 

在此先感谢您。

+0

你想用嵌套'$(document).ready'做什么?我在两年内没有对JS做任何事情,但不是只准备*一次*的文档? – reinierpost 2010-10-22 11:25:59

+0

请忽略嵌套准备,这是意外留在当我玩弄周围试图让它的工作:( – zealisreal 2010-10-22 11:35:39

回答

2

你在ready()中嵌套ready()。不要做这样的事情。试试这个:

$(document).ready(function() { 
    $("#hidden4").click(function (event) { 
     event.preventDefault(); 
     $("#hidden4-1").slideToggle(); 

     var classDown = $('#hidden4 div.down').attr('class'); 
     var classUp = $('#hidden4 div.up').attr('class'); 

     if (classDown == 'down') { 
      $("#hidden4 div.down").attr("class", "up"); 
     } 
     else if (classUp == 'up') { 
      $("#hidden4 div.up").attr("class", "down"); 
     } 
     // Attain the absolute height of the container id container and assign to a usable variable 
     var height = $("#container").height(); 
     alert(height); 
     // Use the previous variable, divide by 4 then round that up and multiply by 4 and assign to new variable 
     var newHeight = Math.ceil(height/4) * 4; 

     // Create a new variable and add the string "center" to it 
     var finalHeight = "center "; 

     // Using the previous variable add to it using the first 2 variables subtracting to find the difference and add 2 
     finalHeight += (newHeight - height) + 2; 

     // Using the previous variable add to it the string "px" for the css selector usage 
     finalHeight += "px"; 

     // Update the CSS of the required element altering the background position with the final variable 
     $(".contentFooter").css('background-position', finalHeight); 
    }); 
}); 

顺便说一句,如果你想在容器高度计算脚本在页面加载和点击执行的,比把代码的函数内部并运行里面都准备好了()的函数,然后点击()。


更新:

$("#foo").slideToggle(function() { 
    // this code executes AFTER slideToggle has completed 
}); 
+0

我做了可悲的尝试之前张贴在这里,它doesnt工作不幸:(任何其他想法? – zealisreal 2010-10-22 11:34:59

+0

另外,我已经做了将该脚本作为在该站点的ready()和click()函数中工作的函数:( – zealisreal 2010-10-22 11:38:35

+0

)警报方法警告的内容是什么? – 2010-10-22 11:48:16

0

我不认为你需要嵌套另一个$(文件)。就绪()的一个jQuery的点击,因为DOM已经准备好当您应用了点击事件。

+0

不幸的是,这不是问题,虽然是错误的,只能由于测试而退出,但删除它并没有解决问题 – zealisreal 2010-10-22 11:37:42