2013-08-18 47 views
1

符號相反我有這樣的代碼在這裏:重新啓動或jQuery的

閱讀以前,大部分代碼是不相關的問題,但它可能更容易理解我想要做的,如果它有可能與否。

$(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語句,我希望代碼重新啓動,如果輸入不正確。有沒有什麼方法告訴javascript回到它說'document.ready()'的地方?

感謝任何anhswers。

回答

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

} 
+0

謝謝。我嘗試了這個,除了我寫了 document.ready(run()) 現在完美地工作 –

0

您可以使用一個小的擴展來實現goto

// <label-name> 
start: alert("aaa"); 
alert("bbb"); 
goto start; 

檢查了這一點,如果你想擁有goto在程序:http://summerofgoto.com/

+0

轉到在JS?真? –

+0

這不是ECMA的一部分,您應該提及需要的第三方腳本。 –

+0

編輯............ – Brian