【发布时间】:2016-02-01 17:53:16
【问题描述】:
代码:
scope.offCanvasShow = function ($event) {
$('.app-off-canvas-menu').toggleClass('show');
// Prevents default functionality of a element
// In this instance, this prevents the anchor from reloading the page on click.
$event.preventDefault();
$event.stopPropagation();
$(document).one('touchstart click', offCanvasHandler);
/**
* Checks to see if the event target isn't .off-canvas-menu or has off-canvas-menu as a parent then removes the
* show class.
* @param event - is the event of the function
*/
function offCanvasHandler(event) {
console.log('hello');
if (!$(event.target).closest('.app-off-canvas-menu').length) {
$('.app-off-canvas-menu').removeClass('show');
$(document).off('touchstart click', offCanvasHandler);
} else {
$(document).one('touchstart click', offCanvasHandler);
}
}
};
这是一个简单的画布外下拉菜单。我有这个奇怪的冒泡问题。我以为我用.one() 函数解决了这个问题。
如果您单击类 app-off-canvas-menu 几次,然后将菜单保持打开状态并在菜单外单击,则菜单将关闭,这就是我想要的。
这里是但是,一旦我在菜单外单击,控制台日志似乎会运行多次,具体取决于我单击 app-off-canvas-menu 汉堡包的次数。
任何人都可以从我的代码中看到任何明显的东西吗?
值得注意的是,我使用的是 angular,因此我可能必须以不同的方式解决这个问题。
【问题讨论】:
标签: javascript jquery angularjs