1

ここにこのコードがあります:

読む前に、ほとんどのコードは質問とは関係ありませんが、可能かどうかにかかわらず、私がやろうとしていることを理解しやすくなるかもしれません。

    $(document).ready(function(){

        AnimateRotate(360, "#box", 0)
        $("#box").animate({   

            top: ((sheight/2)-282)+"px",
            left: ((swidth/2)-307)+"px",
            width: "350px",
            height: "200px"



        }, 2000, function(){

                $('<div>', { id: 'details' }).appendTo('#box').fadeIn(1500)

                $('<input />',{ "class" : "name" }).appendTo("#box").fadeIn(1500)
                $('.name').val($('.name').val() + 'Enter Name here');

                $('<input />',{ "class" : "numb" }).appendTo("#box").fadeIn(1500)
                $('.numb').val($('.numb').val() + 'Enter Age here');

                $('<input type="button" value="Submit">').fadeIn(1500).appendTo(this)   
            .click(function(){ 

                $(this).remove();

                num = $("#box").find(".numb").val();
                nam = $("#box").find(".name").val();

                $("#box").find(".numb").remove();
                $("#box").find(".name").remove();
                $("#details").remove();

                AnimateRotate(-360, "#box", 0)

                $("#box").animate({  

                    top: "150px",
                    left: "100px",
                    width:"0px",
                    height: "0px",
                    padding:"0px"


                    }, 2000, function(){


                        //AnimateRotate(360, "#box", 2000)

                        if (num>0 && num<110 && /^\D+$/.test(nam)) {



                            $("#box").delay(2000).animate({  

                                top: "0px",
                                left: "0px",
                                width: (swidth-100)+"px",
                                height: (sheight-100)+"px",
                                padding: "20px",
                                margin: "20px"



                            }, 2000, function(){


                                $("#box").append('<span class="title">BizAppliance</span>')
                                $('<div>', { id: 'info' }).appendTo('#box')
                                $("#info").append('<span class="num">' + nam + '</span>')
                                $("#info").append('<span class="num"> is </span>')
                                $("#info").append('<span class="num">' + num + '</span>')
                                $("#info").append('<span class="num"> years old</span>')



                            });

                        }else{

                            //GO BACK TO WHERE IT SAYS DOCUMENT.READY()

                        }


                    });


            });
        });


    });

コードの最後に、入力が正しくない場合にコードを再起動する else ステートメントがあります。「document.ready()」と書かれている場所に戻るようにjavascriptに指示する方法はありますか?

回答ありがとうございます。

4

2 に答える 2

2

ハンドラーを別の関数として取り出したところ、機能します。

このコードを使用してください -

$(document).ready(run);

function run() {
    AnimateRotate(360, "#box", 0)
    $("#box")
            .animate(
                    {

                        top : ((sheight / 2) - 282) + "px",
                        left : ((swidth / 2) - 307) + "px",
                        width : "350px",
                        height : "200px"

                    },
                    2000,
                    function() {

                        $('<div>', {
                            id : 'details'
                        }).appendTo('#box').fadeIn(1500)

                        $('<input />', {
                            "class" : "name"
                        }).appendTo("#box").fadeIn(1500)
                        $('.name').val($('.name').val() + 'Enter Name here');

                        $('<input />', {
                            "class" : "numb"
                        }).appendTo("#box").fadeIn(1500)
                        $('.numb').val($('.numb').val() + 'Enter Age here');

                        $('<input type="button" value="Submit">')
                                .fadeIn(1500)
                                .appendTo(this)
                                .click(
                                        function() {

                                            $(this).remove();

                                            num = $("#box").find(".numb").val();
                                            nam = $("#box").find(".name").val();

                                            $("#box").find(".numb").remove();
                                            $("#box").find(".name").remove();
                                            $("#details").remove();

                                            AnimateRotate(-360, "#box", 0)

                                            $("#box")
                                                    .animate(
                                                            {

                                                                top : "150px",
                                                                left : "100px",
                                                                width : "0px",
                                                                height : "0px",
                                                                padding : "0px"

                                                            },
                                                            2000,
                                                            function() {

                                                                // AnimateRotate(360,
                                                                // "#box",
                                                                // 2000)

                                                                if (num > 0
                                                                        && num < 110
                                                                        && /^\D+$/
                                                                                .test(nam)) {

                                                                    $("#box")
                                                                            .delay(
                                                                                    2000)
                                                                            .animate(
                                                                                    {

                                                                                        top : "0px",
                                                                                        left : "0px",
                                                                                        width : (swidth - 100)
                                                                                                + "px",
                                                                                        height : (sheight - 100)
                                                                                                + "px",
                                                                                        padding : "20px",
                                                                                        margin : "20px"

                                                                                    },
                                                                                    2000,
                                                                                    function() {

                                                                                        $(
                                                                                                "#box")
                                                                                                .append(
                                                                                                        '<span class="title">BizAppliance</span>')
                                                                                        $(
                                                                                                '<div>',
                                                                                                {
                                                                                                    id : 'info'
                                                                                                })
                                                                                                .appendTo(
                                                                                                        '#box')
                                                                                        $(
                                                                                                "#info")
                                                                                                .append(
                                                                                                        '<span class="num">'
                                                                                                                + nam
                                                                                                                + '</span>')
                                                                                        $(
                                                                                                "#info")
                                                                                                .append(
                                                                                                        '<span class="num"> is </span>')
                                                                                        $(
                                                                                                "#info")
                                                                                                .append(
                                                                                                        '<span class="num">'
                                                                                                                + num
                                                                                                                + '</span>')
                                                                                        $(
                                                                                                "#info")
                                                                                                .append(
                                                                                                        '<span class="num"> years old</span>')

                                                                                    });

                                                                } else {
                                                                    run();
                                                                }
                                                            });
                                        });
                    });

}
于 2013-08-18T08:37:49.077 に答える