[英]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.