问题描述
我在document.ready中有以下代码()
I have the following code in document.ready()
if ($("#site-master").length > 0) {
setMinContentHeight();
function setMinContentHeight() {
// removed for clarity
}
}
我只是检查页面是否正确(#site-master),然后调用我的最小高度函数,但是我在firebug中遇到以下错误:ReferenceError: setMinContentHeight未定义。
I simply check if the page is correct (#site-master), then call my minimum height function, however I'm getting the following error in firebug: ReferenceError: setMinContentHeight is not defined.
我不是javascript专家,但这怎么可能?如果我将它移到document.ready()之外,该函数可以工作。我已经检查过if语句中的代码了。
I'm no javascript expert, but how can this be? The function works if I move it outside of document.ready(). I have checked and the code inside the if statement is reached.
另外,这是实现我想要的最佳方式吗?
Also, is this the best way of achieving what I want?
提前致谢。
推荐答案
从不在中声明您的功能
或 for
语句:
function setMinContentHeight() {
// removed for clarity
}
if ($("#site-master").length > 0) {
setMinContentHeight();
}
如果我们解决,根据,如果
条款被视为是声明(以及
,,而
,与
,尝试/捕获
等)。
If we address the ECMAScript specification, according to Chapter 12, if
clause is considered to be a Statement (as well as for
, while
, with
, try/catch
, etc).
因此,遵循 Semantics 部分的:
Hence, following the NOTE from Semantics section:
这意味着我们无法保证在这种情况下的一致行为,因此,我们将始终在严格模式中获得异常
如果函数是在语句中声明的。
It means that we cannot guarantee the consistent behavior in such cases, and, as a result, we will always get exception in strict mode
in case if function was declared inside the statement.
这篇关于无法找到Javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!