English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

HTML Referenzhandbuch

HTML-Tag-Übersicht

HTML onfocus-Eigenschaft

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.

HTML Event Attributes

Online-Beispiel

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 ‹/›

Browser Compatibility

IEFirefoxOperaChromeSafari

All major browsers support the onfocus event attribute

Definition and Usage

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>.

HTML 4.01 With HTML5Differences

None.

Syntax

<element onfocus="script">

Attribute Value

ValueDescription
scriptSpecifies the script to be executed when the onfocus event is triggered
HTML Event Attributes