简体   繁体   中英

OnClick and OnKeyPress for standards compliance

I am working on a large website (1600+ pages) that needs upgrading to pass standards compliance. As a result, for every OnClick there has to be, say the Standards, an OnKeyPress handler, so that people not using a mouse can still access the content.

Some tags have an onclick javascript handler. EG:

<a onclick="doSumat();">

Is the following cross browser, working javascript:

<a onclick="doSumat();" onkeypress="this.onclick();" >

Will it work reliably in all browsers and will the result be the same as a mouse click for someone using a screen reader?

W3C - SCR35: Making actions keyboard accessible by using the onclick event of anchors and buttons.

While "onclick" sounds like it is tied to the mouse, the onclick event is actually mapped to the default action of a link or button. The default action occurs when the user clicks the element with a mouse, but it also occurs when the user focuses the element and hits enter or space, and when the element is triggered via the accessibility API.

Source http://www.w3.org/TR/WCAG20-TECHS/SCR35

In this case, why not call doSumat(); with both the onclick and onkeypress handlers?

If you need a this context, then you'll need to use doSumat.call(this); , but you can still place that in both handlers.

<a onclick="doSumat();" onkeypress="doSumat();">

With 1600 pages, I imagine you're trying to find something simple to append to every element with an onclick handler, but shouldn't be that much more complex to define rules to blindly copy everything from an onclick statement to an onkeypress expression.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM