![](/img/trans.png)
[英]How to call $(document).ready(function() {…}); from another file JS
[英]Access function in another js file with $(document).ready(function()
根据这篇文章点击这里查看引用的帖子
我试图按照发布说明访问另一个.js文件中定义的函数。 但是,我仍然有问题。 请参阅下面的代码:
sildemenu.js
$(document).ready(function() {
var window.slideMenu=function(){
//do something here
}();
});
control.js
$(document).ready(function() {
$('#foo').on('click', function() {
window.slideMenu();
});
});
我收到错误“对象[对象窗口]没有方法'sildeMenu'”。 我是编程新手。 请给我一个怜悯。
您尝试定义一个复杂的变量(这种方式是不可能的),而不是为全局对象window
分配一个值。
var window.slideMenu=function(){
//^^^ Get rid of this
//do something here
}();
//^^ and remove this
并摆脱var
固定代码:
window.slideMenu=function(){
//do something here
};
不需要window对象,只需编写:
sildemenu.js
$(document).ready(function() {
slideMenu=function(){
//Do your stuff here!
};
});
control.js
$(document).ready(function() {
$('#foo').on('click', function() {
slideMenu();
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.