[英]Why are these event callbacks being called before the events take place?
I am working with event listeners outside the scope of Angular and jQuery, and I am exploring recursion at the same time. 我正在使用Angular和jQuery范围之外的事件侦听器,并且同时探索了递归。
In this example, I want to add a simple listener to all members of a class that colors the text on mouseover and reverts the color on mouseleave. 在此示例中,我想向类的所有成员添加一个简单的侦听器,以在鼠标悬停时为文本着色并在mouseleave还原颜色。
For each member of the class when button T
is toggled on, 对于班级中的每个成员,按下按钮T
时,
$scope.buttons = document.getElementsByClassName('buttons');
for (var a = 0; a < $scope.buttons.length; a++) {
colorButtons($scope.buttons[a], 'open');
};
and then toggled off 然后关闭
$scope.buttons = document.getElementsByClassName('buttons');
for (var a = 0; a < $scope.buttons.length; a++) {
colorButtons($scope.buttons[a], 'open');
};
I want to call a function that either adds itself as an event listener for each of these elements or executes the coloring needed: 我想调用一个函数,将其自身添加为这些元素中的每一个的事件侦听器或执行所需的着色:
function colorButtons(that, way) {
switch(way) {
case('open'):
that.addEventListener('mouseover', colorButtons(that, null));
that.addEventListener('mouseleave', colorButtons(that, undefined));
break;
case('close'):
that.removeEventListener('mouseover', colorButtons(that, null));
that.removeEventListener('mouseleave', colorButtons(that, undefined));
break;
case(null):
$(that).css('color', '#e6ffff');
break;
case(undefined):
$(that).css('color', '#000000');
break;
}
}
However, I am finding that the callbacks for the event listeners are being called on the initial toggle, not on hover/hover-off, and I am wondering why. 但是,我发现事件监听器的回调是在初始切换时调用的,而不是在悬停/悬停时调用的,我想知道为什么。
Ok, so, one way to solve this problems involves recognizing, first, as @elclanrs points out, that listeners expect functions, not results. 好的,因此,解决此问题的一种方法包括首先确认,正如@elclanrs指出的那样,侦听器期望函数,而不是结果。 Therefore, we cannot pass params into listener callbacks. 因此,我们无法将参数传递给侦听器回调。
Instead, the best way to substitute for params is to create a cache object, ie 相反,替代参数的最佳方法是创建一个缓存对象,即
$scope.buttonCache = { that: {}, way: '' };
and substitute these properties for the params like so: 并将这些属性替换为参数,如下所示:
$scope.buttons = document.getElementsByClassName('buttons');
for (var a = 0; a < $scope.buttons.length; a++) {
$scope.buttonCache.that = $scope.buttons[a];
$scope.buttonCache.way = 'open';
colorButtons();
};
... ...
$scope.buttons = document.getElementsByClassName('buttons');
for (var a = 0; a < $scope.buttons.length; a++) {
$scope.buttonCache.that = $scope.buttons[a];
$scope.buttonCache.way = 'close';
colorButtons();
};
... ...
function colorButtons() {
switch($scope.buttonCache.way) {
case('open'):
$scope.buttonCache.way = null;
$scope.buttonCache.that.addEventListener('mouseover', colorButtons);
$scope.buttonCache.way = undefined;
$scope.buttonCache.that.addEventListener('mouseleave', colorButtons);
break;
case('close'):
$scope.buttonCache.way = null;
$scope.buttonCache.that.removeEventListener('mouseover', colorButtons);
$scope.buttonCache.way = undefined;
$scope.buttonCache.that.removeEventListener('mouseleave', colorButtons);
break;
case(null):
$($scope.buttonCache.that).css('color', '#e6ffff');
break;
case(undefined):
$($scope.buttonCache.that).css('color', '#000000');
break;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.