search
HomeWeb Front-endH5 TutorialHTML5 practice and analysis focus management (activeElement and hasFocus)

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>

JavaScript code

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

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

	alert(document.activeElement.id) // btn
};

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>

JavaScript code

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

	alert(document.hasFocus())  //true
};

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>

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";
	}

};

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
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
Understanding H5 Code: The Fundamentals of HTML5Understanding H5 Code: The Fundamentals of HTML5Apr 17, 2025 am 12:08 AM

HTML5 is a key technology for building modern web pages, providing many new elements and features. 1. HTML5 introduces semantic elements such as, , etc., which enhances web page structure and SEO. 2. Support multimedia elements and embed media without plug-ins. 3. Forms enhance new input types and verification properties, simplifying the verification process. 4. Offer offline and local storage functions to improve web page performance and user experience.

H5 Code: Best Practices for Web DevelopersH5 Code: Best Practices for Web DevelopersApr 16, 2025 am 12:14 AM

Best practices for H5 code include: 1. Use correct DOCTYPE declarations and character encoding; 2. Use semantic tags; 3. Reduce HTTP requests; 4. Use asynchronous loading; 5. Optimize images. These practices can improve the efficiency, maintainability and user experience of web pages.

H5: The Evolution of Web Standards and TechnologiesH5: The Evolution of Web Standards and TechnologiesApr 15, 2025 am 12:12 AM

Web standards and technologies have evolved from HTML4, CSS2 and simple JavaScript to date and have undergone significant developments. 1) HTML5 introduces APIs such as Canvas and WebStorage, which enhances the complexity and interactivity of web applications. 2) CSS3 adds animation and transition functions to make the page more effective. 3) JavaScript improves development efficiency and code readability through modern syntax of Node.js and ES6, such as arrow functions and classes. These changes have promoted the development of performance optimization and best practices of web applications.

Is H5 a Shorthand for HTML5? Exploring the DetailsIs H5 a Shorthand for HTML5? Exploring the DetailsApr 14, 2025 am 12:05 AM

H5 is not just the abbreviation of HTML5, it represents a wider modern web development technology ecosystem: 1. H5 includes HTML5, CSS3, JavaScript and related APIs and technologies; 2. It provides a richer, interactive and smooth user experience, and can run seamlessly on multiple devices; 3. Using the H5 technology stack, you can create responsive web pages and complex interactive functions.

H5 and HTML5: Commonly Used Terms in Web DevelopmentH5 and HTML5: Commonly Used Terms in Web DevelopmentApr 13, 2025 am 12:01 AM

H5 and HTML5 refer to the same thing, namely HTML5. HTML5 is the fifth version of HTML, bringing new features such as semantic tags, multimedia support, canvas and graphics, offline storage and local storage, improving the expressiveness and interactivity of web pages.

What Does H5 Refer To? Exploring the ContextWhat Does H5 Refer To? Exploring the ContextApr 12, 2025 am 12:03 AM

H5referstoHTML5,apivotaltechnologyinwebdevelopment.1)HTML5introducesnewelementsandAPIsforrich,dynamicwebapplications.2)Itsupportsmultimediawithoutplugins,enhancinguserexperienceacrossdevices.3)SemanticelementsimprovecontentstructureandSEO.4)H5'srespo

H5: Tools, Frameworks, and Best PracticesH5: Tools, Frameworks, and Best PracticesApr 11, 2025 am 12:11 AM

The tools and frameworks that need to be mastered in H5 development include Vue.js, React and Webpack. 1.Vue.js is suitable for building user interfaces and supports component development. 2.React optimizes page rendering through virtual DOM, suitable for complex applications. 3.Webpack is used for module packaging and optimize resource loading.

The Legacy of HTML5: Understanding H5 in the PresentThe Legacy of HTML5: Understanding H5 in the PresentApr 10, 2025 am 09:28 AM

HTML5hassignificantlytransformedwebdevelopmentbyintroducingsemanticelements,enhancingmultimediasupport,andimprovingperformance.1)ItmadewebsitesmoreaccessibleandSEO-friendlywithsemanticelementslike,,and.2)HTML5introducednativeandtags,eliminatingthenee

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools