2014-03-12 5 views
1

내가 코드 아래와 같이이 자바 스크립트에서 함수 후 $를 (문서) .ready (함수() {}) 추가 할 수 있습니다 이 코드를 추가하여 다른 코드를 추가하고 작업하십시오. 이 코드를 추가해야합니다.어떻게 내가 원하는 무엇

function() { 
    /* If there are forms, make all the submit buttons in the page appear 
     disabled and prevent them to be submitted again to avoid accidental 
     double clicking. See Issue 980. */ 
    jQuery(function() { 
     /* Delegate the function to document so it's likely to be the last event 
      in the queue because of event bubbling. */ 
     jQuery(document).delegate("form", "submit", function (e) { 
      var form = jQuery(this); 
      if (form.hasClass("form_disabled")) { 
       e.preventDefault(); 
       return false; 
      } 
      else { 
       form 
        .addClass("form_disabled") 
        .find(":submit") 
        .addClass("disabled"); 
      } 
      // Reactivate the forms and their buttons after 3 secs as a fallback. 
      setTimeout(function() { 
       form 
        .removeClass("form_disabled") 
        .find(":submit") 
        .removeClass("disabled"); 
      }, 3000); 
     }); 
    }); 
} 

어떻게 처리 할 수 ​​있습니까? 이 문제를 해결하기 위해 나를 도우십시오.

+0

함수가 무엇을 하는가 : 예를 들어

? 언제 그것을 실행하고 싶습니까? '.click' (예를 들어) 이벤트로 함수를 실행하면 도움이되지 않겠습니까? – zarun

답변

0

document.ready()은 스크립트의 어느 위치에서나 만들 수 있습니다. 모든 코드가 준비 기능에 있어야 할 필요는 없습니다.

당신은 함수의 인스턴스 변수를 작성하고 당신이 필요로하는 곳을 호출 할 수 있습니다 :

$(document).ready(
    var myFunc = function(){ 
    var currentPage = window.location.pathname; 
    //other code 
    } 
    ... 
//and invoke it where you need 
    myFunc(); 
) 
0

첫째, 코드 섹션에 긴 함수 이름, 예를 들어, launchFormControls(). 그런 다음 문서 준비 이벤트 외부에서 함수를 정의하십시오. 좋은 방법은 그렇게하고 준비된 이벤트 본문을 깨끗하게 유지하는 것입니다. 예를 들어

:

function launchFormControls() { 
    //function code 
} 

또는 다른 구문 :

var launchFormControls = function() { 
    //function code 
} 

둘째, 문서 준비 이벤트 내에서 함수를 호출합니다. 문서가로드되면 함수가 정의되고 호출 될 수 있습니다. 이 코드는 자바 스크립트 섹션 또는 파일의 상단 또는 하단에 배치 할 수 있습니다.

$(document).ready(function(){ 
    var currentPage = window.location.pathname; 
    $('#main-menu-list').find('a[href^="' + currentPage+'"]').closest('li').addClass('active'); 

    launchFormControls(); 
}); 
관련 문제