- 12
The focus() method is a JavaScript method that gives focus to an HTML element, if it can be focused1. The focused element is the element that will receive keyboard and similar events by default2.
The focus() method is a method of the HTMLElement interface, which means it can be used on any HTML element that inherits from it, such as input, button, select, etc2.
The syntax of the focus() method is:
element.focus(options);The element is the HTML element to be focused. The options parameter is an optional object that can contain the following properties:
preventScroll: A boolean value indicating whether or not the browser should scroll the document to bring the newly-focused element into view. A value of false (the default) means that the browser will scroll the element into view after focusing it. If preventScroll is set to true, no scrolling will occur2.
focusVisible: A boolean value that should be set to true to force visible indication that the element is focused. By default, or if the property is not true, a browser may still provide visible indication if it determines that this would improve accessibility for users2. This property is experimental and not widely supported by browsers.
The focus() method does not return any value.
Here are some examples of using the focus() method:
// Give focus to an element by its iddocument.getElementById("myText").focus();// Give focus to an element when the page loadswindow.onload = function() {document.getElementById("myText").focus();Découvrir plus d’informations✕Ce résumé a été généré à l’aide de l’IA basée sur plusieurs sources en ligne. Pour afficher les informations sources d’origine, utilisez les liens «En savoir plus ». CSS :focus Selector - W3Schools
Afficher uniquement les résultats de w3schools.comTry It Yourself
The W3Schools online code editor allows you to edit code and view the result in …
Demo
<p><b>My latest discoveries have led me to believe that we are all animals:</b></p>
CSS :first-of-type Selector
W3Schools offers free online tutorials, references and exercises in all the major …
CSS :fullscreen Selector
W3Schools offers free online tutorials, references and exercises in all the major …
HTML DOM Focus
The focus() method gives focus to an element (if it can be focused). See Also: …
jQuery focus() Method
Definition and Usage. The focus event occurs when an element gets focus …
HTMLElement.focus() - Les API Web | MDN - MDN Web Docs
- Autres questions posées
:focus - CSS : Feuilles de style en cascade | MDN - MDN Web Docs
:focus - CSS: Cascading Style Sheets | MDN - MDN Web Docs
:focus | CSS-Tricks - CSS-Tricks
:focus CSS sélecteur | ZONE CSS
Differences between CSS3 :hover and :focus? - Stack Overflow
HTMLElement: focus() method - Web APIs | MDN - MDN Web Docs
Recherches associées pour w3schools focus
- Certains résultats ont été supprimés