在JavaScript中取消onBlur事件? [英] Cancel an onBlur event in JavaScript?

查看:530
本文介绍了在JavaScript中取消onBlur事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为验证值的输入元素设置 onBlur 事件,如果无效,则取消模糊并重新聚焦输入。但是,从 onBlur 返回false并不会像 onClick 那样取消 onBlur 。是否有解决方案(可能使用jQuery?)

I want to set up an onBlur event for an input element that validates the value and, if invalid, "cancels" the blur and refocusses the input. However returning false from onBlur does not cancel the onBlur the way it does with onClick. Is there a solution for this (perhaps using jQuery?)

推荐答案

我不知道任何可靠的跨浏览器方式做这个。通常在 onblur 事件中设置一个小超时,并在计时器触发时调用 focus()

I don't know of any reliable cross-browser way to do this. Usually setting a small timeout in the onblur event and calling focus() when the timer fires works.

例如:

document.getElementById('your_input_id').onblur = function() {
  var self = this;
  setTimeout(function() { self.focus(); }, 10);
}

这篇关于在JavaScript中取消onBlur事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆