Calling a javascript function from another .js file

前端 未结 2 1416
暖寄归人
暖寄归人 2020-12-05 17:55

I have two external .js files. The first contains a function. The second calls the function.

file1.js

$(document).ready(function() {

    function me         


        
2条回答
  •  清歌不尽
    2020-12-05 18:09

    The problem is, that menuHoverStart is not accessible outside of its scope (which is defined by the .ready() callback function in file #1). You need to make this function available in the global scope (or through any object that is available in the global scope):

    function menuHoverStart(element, topshift, thumbchange) {
        // ...
    }
    
    $(document).ready(function() {
        // ...
    });
    

    If you want menuHoverStart to stay in the .ready() callback, you need to add the function to the global object manually (using a function expression):

    $(document).ready(function() {
        window.menuHoverStart = function (element, topshift, thumbchange) {
            // ...
        };
        // ...
    });
    

提交回复
热议问题