Access javascript variable outside of function

后端 未结 1 1234
伪装坚强ぢ
伪装坚强ぢ 2021-01-26 09:22

I have an init() function which creates an image overlay for a map when the html body is loaded. within the function a mapOverlay variable is created. It\'s basically the functi

相关标签:
1条回答
  • 2021-01-26 09:36

    You need to declare the variable outside of the scope of your function to be able to use it in other functions.

    So:

    <script>
      var mapOverlay;
      function init() 
      {
        //stuff
        mapOverlay = new OpenSpace.Layer.MapOverlay("logo");
        //more stuff
      }
      function ChangeImage()
      {
        //stuff
        mapOverlay.setHTML('<img src="$Choice" style="width: 100%; height: 100%; opacity: 0.8;"/>');
      }
    </script>
    
    0 讨论(0)
提交回复
热议问题