【发布时间】:2009-09-19 20:25:25
【问题描述】:
我有几个使用 jquery 的函数,它们在不同的页面上调用。我想知道是否可以在单个文件中定义这些函数,然后在初始文件之后包含的不同文件中调用它们。
这是一个例子:
## default.js
$(function() {
// jquery specific functions here...
function showLoader(update_area) {
update_area.ajaxStart(function() {
update_area.show();
$(this).html('<img id="loading" src="images/loading.gif" alt="loading" />');
});
}
});
## other_file.js (included after default.js)
update_area = $('#someDiv');
showLoader(update_area);
当我这样做时,萤火虫给我'showLoader is not defined。我假设这是因为 $(function() { }); 的范围。其中包含特定于 jquery 的函数。但是,如果是这种情况,这是否意味着我需要在每个调用它们的 js 文件中定义相同的代码?
我怎样才能正确地分开这些东西?
谢谢!
【问题讨论】: