函数在 document.ready 上运行,而不是在被告知时运行
function runs on document.ready instead of when told to
对 jQuery 很陌生。我有一个功能可以做我想要的......几乎。开头是这样的:
var myFunction = function () {
"use strict";
jQuery(function ($) {
//various things
});
};
事情是,我想在另一个函数发生时调用它:
jQuery(function ($) {
"use strict";
$(document).ready(function () {
anotherFunction;
myFunction;
});
});
相反,第一个函数 运行 似乎是在文档准备就绪时自行 ,而 anotherFunction
仅在第二个时刻被调用。
是否取决于第一个函数的开始方式?这是一个好的开始吗?我四处寻找示例和解决方案,但是在 jQuery 中有很多不同的方法来启动一个函数,我不知道哪一种是我需要的。
你能帮我构造这两个函数,让第一个函数不会运行直到第二个调用它吗?
我知道我需要 jQuery(function ($)
位,否则浏览器会告诉我 $ is not a function
...
function one() {
//Do stuff
};
function two() {
one(); //Calls first function
//Do other stuff
}
$(document).ready(function () {
two(); //Executes second function on doc ready
});
这可能会带您到达您想去的地方。也就是说,如果您不希望 任何东西 在 doc ready 上执行,您可以只删除该部分。
对 jQuery 很陌生。我有一个功能可以做我想要的......几乎。开头是这样的:
var myFunction = function () {
"use strict";
jQuery(function ($) {
//various things
});
};
事情是,我想在另一个函数发生时调用它:
jQuery(function ($) {
"use strict";
$(document).ready(function () {
anotherFunction;
myFunction;
});
});
相反,第一个函数 运行 似乎是在文档准备就绪时自行 ,而 anotherFunction
仅在第二个时刻被调用。
是否取决于第一个函数的开始方式?这是一个好的开始吗?我四处寻找示例和解决方案,但是在 jQuery 中有很多不同的方法来启动一个函数,我不知道哪一种是我需要的。
你能帮我构造这两个函数,让第一个函数不会运行直到第二个调用它吗?
我知道我需要 jQuery(function ($)
位,否则浏览器会告诉我 $ is not a function
...
function one() {
//Do stuff
};
function two() {
one(); //Calls first function
//Do other stuff
}
$(document).ready(function () {
two(); //Executes second function on doc ready
});
这可能会带您到达您想去的地方。也就是说,如果您不希望 任何东西 在 doc ready 上执行,您可以只删除该部分。