Global javascript variable inside document.ready

前端 未结 8 1938
感情败类
感情败类 2020-12-08 04:04

Which is the right way of declaring a global javascript variable? The way I\'m trying it, doesn\'t work

$(document).ready(function() {

    var intro;

    i         


        
8条回答
  •  半阙折子戏
    2020-12-08 04:45

    Unlike another programming languages, any variable declared outside any function automatically becomes global,

    
    

    You problem is that you declare variable inside ready() function, which means that it becomes visible (in scope) ONLY inside ready() function, but not outside,

    Solution: So just make it global, i.e declare this one outside $(document).ready(function(){});

提交回复
热议问题