简体   繁体   English

如何在Java脚本中的函数后添加$(document).ready(function(){})

[英]How can i add $(document).ready(function(){}) after a function in java script

What I want to do is I have a code like below : 我想要做的是有如下代码:

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

And now I want to add this code to add and get work with other code. 现在,我想添加此代码以添加和使用其他代码。 I need to add this code after this one: 我需要在这之后添加以下代码:

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

How can I get this done. 我怎样才能做到这一点。 Please help me out to solve this problem. 请帮我解决这个问题。

You can create document.ready() anywhere in script. 您可以在脚本中的任何位置创建document.ready() It is not necessary all of your code should be in ready function. 不必所有代码都准备就绪。

You can create instance variable for function and call it where you need: 您可以为函数创建实例变量,并在需要时调用它:

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

First, name the long function in your code section, for example, launchFormControls() . 首先,在代码部分中命名long函数,例如launchFormControls() And then define the function outside of the document ready event. 然后在文档准备事件之外定义函数。 A good practice would be to do so and keep the ready event body clean. 一个好的做法是这样做,并保持就绪事件的主体清洁。

For example: 例如:

function launchFormControls() {
   //function code
}

Or, in other syntax: 或者,使用其他语法:

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

Second, call your function from within the document ready event. 其次,从文档准备事件中调用函数。 Your function will be defined and able to call once the document is loaded. 一旦加载文档,您的函数将被定义并可以调用。 This code can be placed at the top or bottom of your javascript section or file. 这段代码可以放在您的javascript部分或文件的顶部或底部。

For example: 例如:

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

  launchFormControls();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将一个函数的值转换为文档准备就绪? - How can I get the value from one function into document ready? 如何在$(document).ready之后调用带有参数的函数 - How to call a function with parameters after $(document).ready 如何在$(document).ready(function(){})中使用for循环? - How can I use a for-loop within a $(document).ready(function(){})? 如何将相同的$(document).ready(function()用于多个输入? - How can I use the same $(document).ready(function() for multiple inputs? 如何访问iframe的$(document).ready(function(){})中包含的函数? - How can I access the functions wrapped in the $(document).ready(function(){}) of an iframe? 如何在document.ready的最后调用函数 - How can I call a function at the very end of document.ready Java script 'document on change select function' 和 'document ready function - select change function' - Java script 'document on change select function' and 'document ready function - select change function' 如何在$(document).ready函数中添加多个函数 - How do I add more than one function in my $(document).ready function 我必须在外部脚本中包含$(document).ready(function()多少次 - how many times do I have to include the $(document) .ready (function() in my external script 如何替换$(document).ready函数? - How to replace $(document).ready function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM