简体   繁体   English

使用$(document).ready(function()访​​问另一个js文件中的函数

[英]Access function in another js file with $(document).ready(function()

According to this post Click here to see the referred post 根据这篇文章点击这里查看引用的帖子

I tried to get access to a function which is defined in another .js file following the post instruction. 我试图按照发布说明访问另一个.js文件中定义的函数。 However, I still have a problem. 但是,我仍然有问题。 See my code below: 请参阅下面的代码:

sildemenu.js sildemenu.js

$(document).ready(function() {
    var window.slideMenu=function(){
        //do something here 
    }();
});

control.js control.js

$(document).ready(function() {
    $('#foo').on('click', function() {
         window.slideMenu();
    });
});

I got the error "Object [object Window] has no method 'sildeMenu' ". 我收到错误“对象[对象窗口]没有方法'sildeMenu'”。 I am very new in programming. 我是编程新手。 Please give me a mercy. 请给我一个怜悯。

You try to define a complex variable, (which is impossible this way) instead of assign a value to the global object- window . 您尝试定义一个复杂的变量(这种方式是不可能的),而不是为全局对象window分配一个值。

  var window.slideMenu=function(){
//^^^ Get rid of this
    //do something here 
  }();
 //^^  and remove this

And get rid of the var Fixed code: 并摆脱var 固定代码:

window.slideMenu=function(){
    //do something here 
};

There is no need of window object just write: 不需要window对象,只需编写:

sildemenu.js sildemenu.js

$(document).ready(function() {
    slideMenu=function(){
      //Do your stuff here!
    };
});

control.js control.js

$(document).ready(function() {
    $('#foo').on('click', function() {
         slideMenu();
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM