HTML 中的 onBlur 和 onChange 属性有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/785099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 23:39:13  来源:igfitidea点击:

What is the difference between onBlur and onChange attribute in HTML?

html

提问by pacman

When is one called versus the other? Is there a situation were onChange would be called but onBlur would not be called?

什么时候调用一个而不是另一个?是否存在 onChange 会被调用但 onBlur 不会被调用的情况?

回答by Mark Dickinson

The onBlurevent is fired when you have moved away from an object without necessarily having changed its value.

onBlur当您离开对象而不必更改其值时,将触发该事件。

The onChangeevent is only called when you have changed the value of the field and it loses focus.

onChange只有当您更改了字段的值并且它失去焦点时才会调用该事件。

You might want to take a look at quirksmode's intro to events. This is a great place to get info on what's going on in your browser when you interact with it. His book is good too.

你可能想看看quirksmode 的 intro to events。当您与浏览器交互时,这是获取有关浏览器中正在发生的事情的信息的好地方。他的书也不错。

回答by pacman

onblur fires when a field loses focus, while onchange fires when that field's value changes. These events will not always occur in the same order, however.

onblur 在一个字段失去焦点时触发,而 onchange 在该字段的值改变时触发。然而,这些事件并不总是以相同的顺序发生。

In Firefox, tabbing out of a changed field will fire onchange then onblur, and it will normally do the same in IE. However, if you press the enter key instead of tab, in Firefox it will fire onblur then onchange, while IE will usually fire in the original order. However, I've seen cases where IE will also fire blur first, so be careful. You can't assume that either the onblur or the onchange will happen before the other one.

在 Firefox 中,从更改的字段中跳出会触发 onchange 然后 onblur,它通常会在 IE 中执行相同的操作。但是,如果您按下 Enter 键而不是 Tab,在 Firefox 中它会先触发 onblur 然后 onchange,而 IE 通常会按原始顺序触发。但是,我见过 IE 也会首先触发模糊的情况,所以要小心。您不能假设 onblur 或 onchange 会在另一个之前发生。

回答by Brian Agnew

An example to make things concrete. If you have a selection thus:

一个使事情具体化的例子。如果您有这样的选择:

<select onchange="" onblur="">
  <option>....
</select>

the onblur()is called when you navigate away. The onchange()is called when you select a different option from the selection - i.e. you change what it's currently selected as.

onblur()当您离开时被调用。在onchange()当你从选择不同的选项称为-即你改变就是它的当前选择。

回答by Betty Mock

In Firefox the onchange fires only when you tab or else click outside the input field. The same is true of Onblur. The difference is that onblur will fire whether you changed anything in the field or not. It is possible that ENTER will fire one or both of these, but you wouldn't know that if you disable the ENTER in your forms to prevent unexpected submits.

在 Firefox 中,只有在您使用 Tab 键或在输入字段外单击时才会触发 onchange。Onblur 也是如此。不同之处在于,无论您是否在现场更改了任何内容,onblur 都会触发。ENTER 可能会触发其中一个或两个,但如果您在表单中禁用 ENTER 以防止意外提交,您就不会知道这一点。

回答by php-b-grader

I think it's important to note here that onBlur() fires regardless.

我认为重要的是要注意 onBlur() 无论如何都会触发。

This is a helpful thread but the only thing it doesn't clarify is that onBlur() will fire every single time.

这是一个有用的线程,但唯一没有说明的是 onBlur() 每次都会触发。

onChange() will only fire when the value is changed.

onChange() 只会在值改变时触发。

回答by ólafur Waage

onBlur is when your focus is no longer on the field in question.

onBlur 是当您的注意力不再集中在相关领域时。

The onblur property returns the onBlur event handler code, if any, that exists on the current element.

onblur 属性返回存在于当前元素上的 onBlur 事件处理程序代码(如果有)。

onChange is when the value of the field changes.

onChange 是字段值发生变化的时间。

回答by Sam152

onChange is when something within a field changes eg, you write something in a text input.

onChange 是当字段中的某些内容发生更改时,例如,您在文本输入中写入某些内容。

onBlur is when you take focus away from a field eg, you were writing in a text input and you have clicked off it.

onBlur 是当您将焦点从字段上移开时,例如,您正在输入文本并单击它。

So really they are almost the same thing but for onChange to behave the way onBlur does something in that input needs to change.

所以实际上它们几乎是一样的,但是 onChange 的行为方式 onBlur 在输入需要改变的时候做一些事情。