事件处理程序中的javascript全局变量
我试图请求一个在touchmove函数中全局声明的变量,但得到一个引用错误。有人知道怎么了吗事件处理程序中的javascript全局变量,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我试图请求一个在touchmove函数中全局声明的变量,但得到一个引用错误。有人知道怎么了吗 function drawer(pulltab,drawer){ $('#pulltab').on('touchstart',function(e){ notworking=e.originalEvent.touches[0].pageX; }) $(drawer).on('touchmove',function(loc){ var finger
function drawer(pulltab,drawer){
$('#pulltab').on('touchstart',function(e){
notworking=e.originalEvent.touches[0].pageX;
})
$(drawer).on('touchmove',function(loc){
var fingerloc=loc.originalEvent.touches[0].pageX;
var dist=fingerloc-notworking;
console.log(dist);
if (dist<0){
$(this).css('margin-left',dist);
}
})
$(drawer).on('touchend',function(){
$(this).css('transition','margin-left .1s');
$(this).css('margin-left',0);
})
}
drawer('#pulltab','#navigation-drawer');
功能抽屉(pulltab,抽屉){
$('#pulltab')。在('touchstart',函数(e)上{
notworking=e.originalEvent.touchs[0]。第x页;
})
$(抽屉).on('touchmove',函数(loc){
var fingerloc=loc.originalEvent.touchs[0].pageX;
var dist=指锁不工作;
控制台日志(dist);
如果(区)
我试图请求在touchmove函数中全局声明的变量
引用的代码中没有全局变量声明
假设您尚未声明它,那么您正在\pulltab
上的touchstart
处理程序中创建(但未声明)一个全局变量:
notworking=e.originalEvent.touches[0].pageX;
它使用*来创建一个全局。但是在该代码运行之前,全局将不存在
显然,drawer
上的touchmove
处理程序在#pulltab
上的touchtart
处理程序启动之前启动。由于没有名为notworking
的现有全局调用,因此无法读取其值,并且会出现ReferenceError
。如果#pulltab
上的touchtart
具有exe先剪掉,你不会的
不要依赖于对隐式全局变量的恐惧。声明你的变量。如果你希望它是全局的,就把它放在
var notworking;
…在所有函数之外。(虽然全局变量是件坏事。)™ 最好避免;如果只在drawer
函数中使用notworking
,并且不需要在调用drawer
时共享它,只需在drawer
中声明它即可。使用它时,您可能还需要检查它是否有有用的值
*(这是我贫血小博客上的一篇帖子)
我试图请求在touchmove函数中全局声明的变量
引用的代码中没有全局变量声明
假设您尚未声明它,那么您正在\pulltab
上的touchstart
处理程序中创建(但未声明)一个全局变量:
notworking=e.originalEvent.touches[0].pageX;
它使用*来创建一个全局。但是在该代码运行之前,全局将不存在
显然,drawer
上的touchmove
处理程序在#pulltab
上的touchtart
处理程序启动之前启动。由于没有名为notworking
的现有全局调用,因此无法读取其值,并且会出现ReferenceError
。如果#pulltab
上的touchtart
具有exe先剪掉,你不会的
不要依赖于对隐式全局变量的恐惧。声明你的变量。如果你希望它是全局的,就把它放在
var notworking;
…在所有函数之外。(虽然全局变量是件坏事。)™ 最好避免;如果只在drawer
函数中使用notworking
,并且不需要在调用drawer
时共享它,只需在drawer
中声明它即可。使用它时,您可能还需要检查它是否有有用的值
*(这是我贫血小博客上的一篇帖子)我在那段代码中没有看到任何全局变量声明。我在那段代码中没有看到任何全局变量声明。