onfocus="this.blur()" is hated by blind webmasters

onfocus="this.blur()" is hated by blind webmasters

When talking about the screen reading software operation page, he particularly emphasized to our front-end students: what he hates most is the code onfocus="this.blur()" on the page link. Where did this come from?

各種瀏覽器虛線框差異圖

(Figure 1)

Dear students, does this code look familiar to you? Yes, you know, we often use it to remove the dotted frame that appears around a link when it is in focus (as shown in Figure 1 above). If you google it, you will find that the first few dozen pages are all about the technique of removing the dotted frame. But we may have never thought about this before: this line of code has caused great trouble for blind users: it interrupts the Tab key path of blind users, causing the Tab cursor to be unable to focus on the next controller (link, form field, object, image map, etc.) of the page. The test is as follows:


Copy code
The code is as follows:

<body>
<a href="#" >First link</a>
<a href="#" >Second link</a>
<a href="#" onfocus="this.blur();">Third link</a>
<a href="#" >Fourth link</a>
<a href="#" >Fifth link</a>
<a href="#" >Sixth link</a>
</body>

When you press the Tab key, the first and second links can get the focus normally. When you continue to tab to the third link, tragedy occurs: the focus returns to the first link and cannot be focused on the fourth link. The reason is that when the focus is on the third link, the onfocus="this.blur()" event processing forces the focus to be lost, and the focus returns to the beginning of the document. The result of continuously pressing Tab is that the focus will rotate between the first three links, and the content behind them cannot be accessed using the Tab key [1].

虛線框影響視覺效果 (Figure 2)

So, is there a better way? Basically, adding onfocus="this.blur()" is to remove the dotted frame around the link after it gets the focus (of course, the focus effects in Chrome, Safari, and Opera are different, so let's just call it that here). The W3C article on Outline explains that this dotted box is used to tell users which element on the current page has focus. I think the existence of the dotted box is reasonable, but sometimes you may not be able to avoid certain "visual cleanliness" needs (as shown in Figure 2: the dotted box separates the "product" background from the red block below). The following summarizes several common methods for removing the dotted box:

How to remove the dotted frame Pros and Cons compatibility Whether to interrupt the tab
<a href="#" onfocus="this.blur()">this blur</a> Link focus triggers when losing focus, js and html are coupled together No compatibility issues yes
a:focus {outline:none} or
a{outline:none}
Outline was introduced in CSS 2.1. It is the responsibility of CSS to remove the visual problems of the dotted box. ie6/ie7 not supported, ie8+/ff/safari/opera[2] supported no
<a href=”#” hidefocus=”true” >hidefocus</a> This property is a private property of IE.[3] IE5+ support no
a { noFocusLine: expression(this.onFocus = this.blur())} Batch processing is possible, but the performance of expression cannot be ignored Expression is supported by IE6/7, but not by IE8+ or non-IE yes

Based on the above, the recommended method to remove the dotted frame of the link is: use the hidefocus attribute in IE, and use outline:none in FF/chorme/opera/safari. Right now:
<a href="#" hidefocus="true" >Link</a>
a:focus {
outline:none;
}

Yang Yongquan said helplessly that if the tab cannot access the entire content of the page because of onfocus="this.blur()", the screen reader software will forcibly filter out this attribute before reading the page. However, if the user dynamically triggers this.blur() in js, the screen reader software will have to come up with new tricks to restrain it. This undoubtedly increases the workload of screen reading software development. In order to allow blind users to access our website more smoothly, try to avoid using onfocus="this.blur()".

Notes

[1] By default, in Safari, pressing the tab key will not focus links, but will focus form fields. You can enable this feature by checking "Press tab to highlight each item on the webpage" in Preferences-Advanced. Opera is quite special. It can focus the page up, down, left, and right by using the shift+up, down, left, and right arrow keys.

[2] In Opera, no dotted frame will appear when clicking a link (in focus or active state), so the dotted frame issue for links in Opera can be ignored. The wireframe created by Opera using the shift+up, down, left, and right keys cannot be removed by using outline:none, but Opera supports the outline attribute.

[3]The hidefocus attribute is a private attribute of IE. Although the hidefocus attribute has two values: true or false, the test results show that in IE5-IE9, regardless of whether the value is true or false, as long as the hidefocus attribute is added, the link will lose the dotted frame.

<<:  How to prevent hyperlinks from jumping when using a link

>>:  What are mysql dirty pages?

Recommend

Detailed explanation of MySQL Explain

In daily work, we sometimes run slow queries to r...

Nginx location matching rule example

1. Grammar location [=|~|~*|^~|@] /uri/ { ... } 2...

18 sets of exquisite Apple-style free icon materials to share

Apple Mug Icons and Extras HD StorageBox – add on...

Use docker to deploy tomcat and connect to skywalking

Table of contents 1. Overview 2. Use docker to de...

Detailed steps for installing nodejs environment and path configuration in Linux

There are two ways to install nodejs in linux. On...

How to install Linux flash

How to install flash in Linux 1. Visit the flash ...

JavaScript function syntax explained

Table of contents 1. Ordinary functions 2. Arrow ...

A simple way to achieve scrolling effect with HTML tag marquee (must read)

The automatic scrolling effect of the page can be...

Solution to mysql login warning problem

1. Introduction When we log in to MySQL, we often...

Mini Program to Implement Text Circular Scrolling Animation

This article shares the specific code of the appl...

MySQL uses custom sequences to implement row_number functions (detailed steps)

After reading some articles, I finally figured ou...

Native js to realize the upload picture control

This article example shares the specific code of ...

How to uninstall MySQL 5.7.19 under Linux

1. Find out whether MySQL was installed before Co...

Implementation of CSS3 button border animation

First look at the effect: html <a href="#...