jQuery - $(document).ready 函数中的函数
像这样在 .ready() 内部创建函数是否正确
$(document).ready(function() {
:
$(document).ready(function() {
function callMe() {
}
});
在 DOM 准备好并且在 ready() 内部发生事件之前,
.ready()
内部的函数不必调用> 被触发。
只是为了澄清一点 - 这是说明问题的代码:
$(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
我需要调用的函数需要 ind
变量的更新值 - 我想我可以将其作为参数传递,但是有更好的方法吗?
另外 - 另一件重要的事情是,所讨论的 function() 还可以更改 ind 变量的值 - 例如递增它 (ind++
)。
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.
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?
Also - another important thing is that the function()
in question can also change the value of the ind
variable - for instance incrementing it (ind++
).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
是的,您可以这样做,这只是范围。
如果您只需要从
$(document).ready(function() { })
中访问callMe()
,那么可以将函数放在那里,并提供某些架构的好处是因为您无法访问该上下文之外的函数。如果您需要在文档就绪之外使用
callMe()
函数,则需要在该上下文之外定义callMe()
函数。更新
根据您的澄清,您有两个选择:
1)在
ready()
外部声明变量,但然后在ready()
内部定义变量:2) 在
内>ready()
,使用window.yourVariable = 'whatever'; 定义变量
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.If you need to use the
callMe()
function outside of document ready though, you need to define thecallMe()
function outside of that context.UPDATE
Based on your clarification, you have two options:
1) DECLARE variable outside of
ready()
, but then define variable inside ofready()
:2) Within
ready()
, define variables usingwindow.yourVariable = 'whatever';
这也会起作用。
如果您使用
它可能不起作用,并且可能会收到错误“函数未定义”
This will also work.
If you use
It may not work and you may get an error "function is undefined"
您可以这样做:
因此,您不需要将函数放在准备好的文档之外,并且代码会变得分组且更有组织。 ;)
You can do like that:
So, you don't need to put the function outside the document ready and the code becomes grouped and more organized. ;)
直接调用该函数可能是一个更好的主意,如下所示:
It is probably a better idea to call the function directly like so:
当您在
$(document).ready
中创建函数时,可以保证在文档加载之前不会调用该函数。当然,它只能从该事件处理程序本身(事件处理程序稍后的某个位置)调用。换句话说,你想要做的事情是有效的(尽管不一定是可取的——你必须透露更多关于你想要完成的事情)。
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).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).
这绝对是合法的。问题是你为什么要这么做?可能是将函数的范围绑定到ready的范围,而不是将其全局绑定到window对象。但这是你真正想要的吗?我建议看看 javascript 中的函数闭包以及它如何处理作用域。帮助澄清它的需要......
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...
看看是否可以通过将标记移动到 html 文件的底部来消除使用 document.ready 的需要。这应该会让事情变得简单很多。否则,在 document.ready 范围之外声明该函数,然后在 document.ready 函数范围内调用它。
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.
您可以像下面这样使用:
您可以从任何事件中调用此函数。
You can use like the following:
You can call this function from any event.