English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Die onfocus-Eigenschaft wird verwendet, um den Event-Handler für das focus-Ereignis des aktuellen Elements abzurufen oder zu setzen. Das focus-Ereignis wird ausgelöst, wenn das aktuelle Element die Tastaturbezugnahme erhält.
Führt das Skript aus, wenn das Eingabefeld den Fokus erhält:
<!DOCTYPE html> <html> <head> <title>Verwendung der HTML onfocus-Eigenschaft (Grundtutorialsite oldtoolbag.com)</title> <script> function setStyle(x) { document.getElementById(x).style.background="yellow"; } </script> </head> <body> <p>Wenn das Eingabefeld den Fokus erhält, wird die Funktion setStyle ausgelöst. Diese Funktion ändert die Hintergrundfarbe des Eingabefelds.</p> Erster Name: <input type="text" id="fname" onfocus="setStyle(this.id)"><br> Letzter Name: <input type="text" id="lname" onfocus="setStyle(this.id)"> </body> </html>Test it out ‹/›
IEFirefoxOperaChromeSafari
All major browsers support the onfocus event attribute
The onfocus attribute is triggered when an element gains focus.
Onfocus is usually used with <input>, <select>, and <a>.
Hint:The attribute event opposite to onfocus isonblur .
Note: The onfocus attribute cannot be used with the following elements: <base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style>, or <title>.
None.
<element onfocus="script">
Value | Description |
---|---|
script | Specifies the script to be executed when the onfocus event is triggered |