当我尝试将日志转储到Web页面时,这真的很奇怪。
我有基本的应用程序(在xcode中)与网页插件,允许我从iPhone的日志到网页。
但是,不知怎么的,当我试图调用放置在其他js文件中的方法时,我得到:“方法”是没有定义的。
xcode-Web结构:

Socket.html片段:
<script type="text/javascript" src="src/js/script.js"></script>
<script type="text/javascript">
$(document).ready(main);
// Run when document.ready fires
function main() {
$('#btnClear').click(function() {
clearTable();
});
}
....
</script>clearTable是在src/js/script.js文件中定义的方法,我知道它已经加载,因为onLoad方法已经调用了。
Script.js片段:
$(function() {
....
function onLoad(){
....
}
function clearTable(){
....
}
onLoad();
});有人知道原因吗?
我把这个项目处理到linux上,一切都很好。所有的依赖都很好。
谢谢,
发布于 2013-03-25 13:21:40
由于作用域问题,clearTable是在匿名函数中定义的,因此只能在该范围内使用。
您正在尝试从另一个不可用的范围调用它。
解决方案是在全局范围内定义clearTable。例如
$(function() {
// ....
function onLoad() {
// ....
}
window.clearTable = function() {
// ....
}
onLoad();
});问题:小提琴
解决方案:小提琴
另一种解决办法
var clearTable, isAutoScroll; //Declare these as global variables
$(function() {
// ....
function onLoad() {
// ....
}
//note this is a function variable and there is no `var`
clearTable = function() {
// ....
}
//note there is not `var` used while defining the variable
isAutoScroll = false;
onLoad();
});https://stackoverflow.com/questions/15615940
复制相似问题