0

我试图找出如何让 jQuery 脚本在单击事件启动时正常工作,该脚本计算容器的高度(此脚本出现在加载页面的开头并 100% 工作)但我是现在试图让它在 .click 事件中工作。

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

ps 我已经尝试过准备好文件;等待 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);
    });
});

先感谢您。

4

2 回答 2

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

顺便说一句,如果您希望容器高度计算脚本在页面加载和单击时都执行,则将代码放在函数中并在 ready() 和 click() 中运行该函数。


更新:

$("#foo").slideToggle(function() {
    // this code executes AFTER slideToggle has completed
});
于 2010-10-22T11:24:14.617 回答
0

我认为您不需要在 jquery 点击中嵌套另一个 $(document).ready() ,因为当您应用点击事件时 DOM 已经准备好。

于 2010-10-22T11:30:12.783 回答