我想找出如何让一个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);
});
});
在此先感谢您。
你想用嵌套'$(document).ready'做什么?我在两年内没有对JS做任何事情,但不是只准备*一次*的文档? – reinierpost 2010-10-22 11:25:59
请忽略嵌套准备,这是意外留在当我玩弄周围试图让它的工作:( – zealisreal 2010-10-22 11:35:39