Table of Contents
1. document.activeElement property
HTML code
JavaScript code
2. document.hasFocus() method
HasFocus() application example
Home Web Front-end H5 Tutorial HTML5 practice and analysis focus management (activeElement and hasFocus)

HTML5 practice and analysis focus management (activeElement and hasFocus)

Feb 10, 2017 pm 02:57 PM

Nowadays, websites are paying more and more attention to people with disabilities. Many websites have begun to create some convenient channels for people with poor vision to facilitate them to browse the web. Below I will introduce to you some things about focus management and websites for blind people. I hope it will be helpful to you.

Websites in the 21st century are paying more and more attention to people with disabilities. Other types of disabilities are easier to say. If people with poor eyesight go to browse the website, they basically don’t know how to browse. People with poor eyesight basically rely on getting focus to browse the website, and mainly rely on getting the focus to read the content to browse the website. So focus management is particularly important when making websites for people with poor vision.

Precisely because people with poor eyesight exist objectively, HTML5 has added a function to assist in managing DOM focus.

1. document.activeElement property

The Document.activeelement property always refers to the element in the DOM that currently has the focus. Elements gain focus through user input (usually pressing the Tab key), calling the focus() method in code, and page loading. Let’s look at a small example first.

HTML code

<body id="body">
  <input id="btn" type="button" value="梦龙小站" />
</body>
Copy after login
Copy after login

JavaScript code

window.onload = function(){
	var btn = document.getElementById("btn");

	//页面加载获取焦点
	alert(document.activeElement.id) // body
	
	//调用focus()方法获取焦点
	btn.focus();

	alert(document.activeElement.id) // btn
};
Copy after login

By default, when the document has just been loaded, the reference to the document.body element is saved in document.activeelement. During document loading, the value of document.activeelement is null. You can use document.activeelement to determine whether the document is loaded.

2. document.hasFocus() method

In addition to the newly added document.activeelement attribute, HTML5 also adds the document.hasfocus() method. This method is used to determine whether the document has focus. Let’s look at a small example first.

HTML code

<body id="body">
  <input id="btn" type="button" value="梦龙小站" />
</body>
Copy after login
Copy after login

JavaScript code

window.onload = function(){
	var btn = document.getElementById("btn");

	alert(document.hasFocus())  //true
};
Copy after login

With the hasFocus() method, we can detect whether the document has gained focus and know whether the user is interacting with the page. .

The most important use of querying a document to know which element has received focus, and determining whether a document has received focus, is to provide accessibility to web applications. One of the main hallmarks of an accessible web application is proper focus management, and knowing exactly which element has focus is a huge improvement. At least we don't have to guess as much as we used to. Let’s look at a small example first.

HasFocus() application example

HTML code

<p id="meng">鼠标放上来</p>
<p id="long" style="display:none;">获取焦点了</p>
Copy after login

JavaScript code

window.onload = function(){
	var oMeng = document.getElementById("meng");
	var oLong = document.getElementById("long");

	oMeng.onmouseover = getFocus;
	oMeng.onmouseout = loseFocus;

	function getFocus(){
		if (document.hasFocus())
		{
			oLong.style.display = "block";
		}
	}
	function loseFocus(){
		oLong.style.display = "none";
	}

};
Copy after login

The above example makes full use of the hasFocus() method to determine whether to obtain focus. This also allows us to feel the charm of the hasFocus() method and the usefulness of focus management. Browsers that can implement this hasFocus() method and activeElement attribute are: FireFox 3+, Safari 4+, Chrome, Opera 8+ and IE 4+.

HTML5 actual combat and analysis of focus management (activeElement and hasFocus) are shared here with everyone. The accessibility of web applications in China still needs to be developed. If you master focus management (activeElement and hasFocus) well, you can basically achieve the accessibility of web applications. Thank you for your support to Menglong Station. For more updates about HTML5, please pay attention to the relevant updates of the actual combat and analysis of HTML5 in Menglong Station.


The above is the content of HTML5 actual combat and analysis of focus management (activeElement and hasFocus). For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

See all articles