mootools 禁用或拦截添加的事件

发布于 2024-12-25 09:01:38 字数 674 浏览 2 评论 0原文

假设我这样做:

$('field').addEvents ({
  'focus' : function() { // do some stuff }
  'blur' : function() { // do other stuff }
});

这是我所有文本输入字段的默认行为。我现在想做的是这样的

然后:

$('shinyButton').addEvent('click', function() {
  stopDefaultBlurFunctionInCurrentlyFocussedField();
  // do seriously cool stuff
  if (finishedDoingSeriouslyCoolStuff) {
    $('field').focus();  // return focus to input field
  }
}

所以:

1)我如何 stopDefaultBlurFunctionInCurrentlyFocussedField() ;?

2)我如何判断我是否真的完成了DoingSeriouslyCoolStuff?

let's say i do this:

$('field').addEvents ({
  'focus' : function() { // do some stuff }
  'blur' : function() { // do other stuff }
});

this is the default behaviour for all my text input fields. what i now want to do is something like this

<button id='shinyButton' name='shinyButton'>Poke Me</button>

then:

$('shinyButton').addEvent('click', function() {
  stopDefaultBlurFunctionInCurrentlyFocussedField();
  // do seriously cool stuff
  if (finishedDoingSeriouslyCoolStuff) {
    $('field').focus();  // return focus to input field
  }
}

so:

1) how do i stopDefaultBlurFunctionInCurrentlyFocussedField();?

2) how do i tell if i'm actually finishedDoingSeriouslyCoolStuff?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愿得七秒忆 2025-01-01 09:01:38

严格来说,您无法执行您想要执行的操作,因为 blur 事件在您的点击处理程序执行之前触发。

Dimitar 关于禁用鼠标悬停事件的建议对于鼠标用户来说效果很好,但会阻止用户使用键盘触发按钮。

一种选择(但有点黑客)是在模糊事件处理程序中引入微小的延迟,并使用变量标志来控制事件触发(您可能需要调整延迟,以便它是难以察觉,但对于您的目的来说仍然足够长:

var disableBlurMethod = false;

$('field').addEvents ({
  'focus' : function() { // do some stuff }
  'blur' : function() {
    (function() {
      if (!disableBlurMethod) {
        // do some stuff
      }
    }).delay(50);
  }
});

$('shinyButton').addEvent('click', function() {
  disableBlurMethod = true;
  // do seriously cool stuff
  if (finishedDoingSeriouslyCoolStuff) {
    disableBlurMethod = false;
    $('field').focus();  // return focus to input field
  }
}

Strictly speaking you can't do what you want to do because the blur event fires before your click handler does.

Dimitar's suggestion of disabling the event on mouseover works fine for mouse users, but prevents users from triggering the button with the keyboard.

One option (but a bit of a hack) would be to introduce a tiny delay into the blur event handler, and use a variable flag to control the event firing (you might need to tune the delay so it's imperceptible but still long enough for your purposes:

var disableBlurMethod = false;

$('field').addEvents ({
  'focus' : function() { // do some stuff }
  'blur' : function() {
    (function() {
      if (!disableBlurMethod) {
        // do some stuff
      }
    }).delay(50);
  }
});

$('shinyButton').addEvent('click', function() {
  disableBlurMethod = true;
  // do seriously cool stuff
  if (finishedDoingSeriouslyCoolStuff) {
    disableBlurMethod = false;
    $('field').focus();  // return focus to input field
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文