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

ところで、container-height-calculationスクリプトをページの読み込み時とクリック時の両方で実行する場合は、コードを関数内に配置し、ready()とclick()の両方で関数を実行します。


アップデート:

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

クリックイベントを適用したときにDOMはすでに準備ができているので、jqueryクリックで別の$(document).ready()をネストする必要はないと思います。

于 2010-10-22T11:30:12.783 に答える