Recommended courses:
onblur event
Definition and Usage
occurs when an object loses focus.
umgekehrt. The onblur event is most commonly used with form validation code (for example, when the user leaves a form field). onfocus-Ereignisevent is similar to
umgekehrt. Hinweis: onfocusout-Ereignisevent is similar to addEventListener() Methodeto achieve this. The main difference is that the onblur event does not bubble. Therefore, if you want to find out whether an element or its child elements have lost focus, you can use the onfocusout event. However, you can use the onblur event's
Beispiel
Execute JavaScript when the user leaves the input field:
<input type="text" onblur="myFunction()">
There are more TIY examples at the bottom of the page.
Syntax
In HTML:
<element onblur="myScript">
In JavaScript:
object.onblur = function(){}myScript};
Verwenden Sie die Methode addEventListener() in JavaScript:
object.addEventListener("blur", myScript);
Anmerkung:Internet Explorer 8 und frühere Versionen unterstützen dies nicht addEventListener() Methode.
Technische Details
Blasen: | Nicht unterstützt |
---|---|
Kann abgebrochen werden: | Nicht unterstützt |
Ereignis-Typ: | FocusEvent |
Unterstützte HTML-Tags: | Alle HTML-Elemente, außer: <base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style> und <title> |
DOM-Version: | Level 2 Ereignisse |
Browser-Unterstützung
Ereignis | Chrome | IE | Firefox | Safari | Opera |
---|---|---|---|---|---|
onblur | Unterstützt | Unterstützt | Unterstützt | Unterstützt | Unterstützt |
Mehr Beispiele
Beispiel
Verwenden Sie "onblur" und "onfocus"-Ereignisse zusammen:
<input type="text" onfocus="focusFunction()" onblur="blurFunction()">
Beispiel
Event Delegation: Setzen Sie den useCapture-Parameter von addEventListener() auf true:
<form id="myForm"> <input type="text" id="myInput"> </form> <script> var x = document.getElementById("myForm"); x.addEventListener("focus", myFocusFunction, true); x.addEventListener("blur", myBlurFunction, true); function myFocusFunction() { document.getElementById("myInput").style.backgroundColor = "yellow"; } function myBlurFunction() { document.getElementById("myInput").style.backgroundColor = ""; } </script>
Beispiel
Event Delegation: Verwenden Sie das "focusin"-Ereignis (wird von Firefox nicht unterstützt):
<form id="myForm"> <input type="text" id="myInput"> </form> <script> var x = document.getElementById("myForm"); x.addEventListener("focusin", myFocusFunction); x.addEventListener("focusout", myBlurFunction); function myFocusFunction() { document.getElementById("myInput").style.backgroundColor = "yellow"; } function myBlurFunction() { document.getElementById("myInput").style.backgroundColor = ""; } </script>