在DOM加载.click事件后,jQuery脚本工作?

时间:2010-10-22 11:19:48

标签: jquery jquery-click-event

我试图找到如何让jQuery脚本在启动单击事件时正常工作,脚本计算容器的高度(此脚本在加载时出现在页面的开头,并且100%工作)但我现在正试图让它在.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);
    });
});

提前谢谢。

2 个答案:

答案 0 :(得分: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);
    });
});

btw,如果你想在页面加载和点击时同时执行容器高度计算脚本,那么将代码放在一个函数中并在ready()和click()中运行该函数。


更新:

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

答案 1 :(得分:0)

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