jquery全局变量accesibilty

NMN*_*NMN 0 javascript jquery

如何创建一个全局变量来获取div的初始位置,甚至可以访问所有函数

$(document).bind('click', function (e) {
Run Code Online (Sandbox Code Playgroud)

或者可以以下功能

$(document).bind('click', function (e) { //blahblah });
Run Code Online (Sandbox Code Playgroud)

在里面访问

$(document).ready(function(){});
Run Code Online (Sandbox Code Playgroud)

moh*_*ias 5

在document.ready之外声明一个变量或将其附加到window对象.

var globalVariable;
$(document).ready(function(){
     globaVariable = $("div").offset().top;
    // set the position of div as you desire

});
Run Code Online (Sandbox Code Playgroud)

否则使用 window.globalVariable

 $(document).ready(function(){
     window.globaVariable = $("div").offset().top;
    // set the position of div as you desire

});
Run Code Online (Sandbox Code Playgroud)