jQuery - $(document).ready 函数中的函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6780890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery - function inside $(document).ready function
提问by user398341
Is it correct to create functions inside of
在内部创建函数是否正确
$(document).ready(function() {
like so:
像这样:
$(document).ready(function() {
function callMe() {
}
});
The function inside of the .ready()
does not have to call before DOM is ready and event inside of the ready()
is triggered.
内部的函数.ready()
不必在 DOM 准备好并ready()
触发内部的事件之前调用。
Just to clarify a little bit - here's the code which would illustrate the problem:
只是为了澄清一点 - 这是可以说明问题的代码:
$(function() {
var ind = 0;
// some event is executed and changes the value of the ind
// another event which affects the ind variable
// and another one - after this event we call our function
// there's another event - and we call our function again
The function which I need to call needs the updated value of the ind
variable - which I guess I could pass as a parameter, but is there a better way of doing it?
我需要调用的函数需要ind
变量的更新值- 我想我可以将其作为参数传递,但是有没有更好的方法呢?
Also - another important thing is that the function()
in question can also change the value of the ind
variable - for instance incrementing it (ind++
).
另外 - 另一件重要的事情是有function()
问题的也可以改变ind
变量的值- 例如增加它(ind++
)。
回答by Mike Richards
Yes, you can do that, it's just a matter of scope.
是的,您可以这样做,这只是范围问题。
If you only need to access callMe()
from within $(document).ready(function() { })
, then it's fine to put the function there, and offers some architecture benefits because you can't access the function outside of that context.
如果您只需callMe()
要从 inside访问$(document).ready(function() { })
,那么可以将函数放在那里,并提供一些架构优势,因为您无法在该上下文之外访问该函数。
If you need to use the callMe()
function outside of document ready though, you need to define the callMe()
function outside of that context.
如果您需要callMe()
在文档就绪之外使用该函数,则需要callMe()
在该上下文之外定义该函数。
function callMe() {
// Do Something
}
$(document).ready(function() {
callMe();
});
UPDATE
更新
Based on your clarification, you have two options:
根据您的说明,您有两种选择:
1) DECLARE variable outside of ready()
, but then define variable inside of ready()
:
1) 在 之外声明变量ready()
,然后在 内部定义变量ready()
:
var someVariable;
function callMe() {
someVariable++;
alert(someVariable);
}
$(document).ready(function() {
someVariable = 3;
callMe(); // Should display '4'
});
2) Within ready()
, define variables using window.yourVariable = 'whatever';
2) 在 内ready()
,使用定义变量window.yourVariable = 'whatever';
回答by Daniel James Canil
This will also work.
这也将起作用。
$(document).ready(function() {
callMe = function() {
alert('hello');
}
});
callMe();
If you use
如果你使用
var callMe = function () { ... }
It may not work and you may get an error "function is undefined"
它可能不起作用,您可能会收到错误“函数未定义”
回答by rntperes
You can do like that:
你可以这样做:
$(document).ready(function(){
var callMe = function(){
//enter code here
}
$(".my-class").on("click", function(){
callMe();
});
});
So, you don't need to put the function outside the document ready and the code becomes grouped and more organized. ;)
因此,您不需要将功能准备好放在文档之外,代码变得分组且更有条理。;)
回答by Tomas Kohl
When you create a function inside $(document).ready
, it's guaranteed that it won't be called before the document has loaded. Of course, it can only be called from that event handler itself (somewhere later in the event handler).
当您在 中创建函数时$(document).ready
,可以保证在文档加载之前不会调用它。当然,它只能从该事件处理程序本身调用(在事件处理程序后面的某个地方)。
In other words, what you're trying to do is valid (though not necessarily desirable - you'd have to reveal more about what you are trying to accomplish).
换句话说,你试图做的事情是有效的(虽然不一定是可取的 - 你必须透露更多关于你试图完成的事情)。
回答by soulBit
It is probably a better idea to call the function directly like so:
像这样直接调用函数可能是一个更好的主意:
$(document).ready(myFunction);
function myFunction() {
// Your code here
}
回答by PhD
This is definitely legal. The question is why do you want to do it? Probably to bind the function's scope to that of ready and not have it globally bound to the window object. But is that what you really want? I suggest having a look on function closures in javascript and how it handles scoping. to help clarify the need for it...
这绝对是合法的。问题是你为什么要这样做?可能是将函数的作用域绑定到 ready 的作用域,而不是将其全局绑定到 window 对象。但这真的是你想要的吗?我建议看看 javascript 中的函数闭包以及它如何处理范围。帮助澄清对它的需求......
回答by Manas Yadav
See if you can eliminate the need for using document.ready by moving your tag to the bottom of the html file. This should make things alot simpler. Otherwise, declare the function outside the scope of the document.ready and just call it in the document.ready function scope.
看看你是否可以通过将你的标签移动到 html 文件的底部来消除使用 document.ready 的需要。这应该会让事情变得简单很多。否则,在 document.ready 范围之外声明函数,并在 document.ready 函数范围内调用它。
回答by vinod raman
You can use like the following:
您可以像下面这样使用:
$(document).ready(function () {
printReport = function(rowIndex) {
// Your code here
}
});
You can call this function from any event.
您可以从任何事件调用此函数。