Home > Article > Web Front-end > How Can I Use JavaScript's focus() Function to Make a DIV Focusable?
Focusable DIVs Using JavaScript's focus() Function
While the focus() function commonly operates on form elements like input fields or buttons, it is also possible to focus on a
Assigning Tabindex
<br><div id="tries" tabindex="0">You have 3 tries left</div><br>
A tabindex of 0 places the div in the page's natural tab order. Positive values specify a specific priority within the tab order. Meanwhile, a value of -1 makes the div focusable only via script, not user input.
Example Code
<br>document.getElementById('test').onclick = function () {</p> <p>};<br>
This script demonstrates setting focus to a div with a tabindex of -1 through a user-triggered event.
Styling Focused DIVs
To visually indicate fokus, CSS can be used:
<br>div:focus {</p> <p>}<br>
By implementing these techniques, you can successfully focus on
The above is the detailed content of How Can I Use JavaScript's focus() Function to Make a DIV Focusable?. For more information, please follow other related articles on the PHP Chinese website!