问题
So, I'm testing all different HTML tags and attributes, freshing things up a bit, and for some reason, accesskey does not appear to be working.
The accesskey attribute is used to assign a key to an element. Depending on your OS, pressing Alt+that key (Windows) or Cmd+that key (Mac) will give focus to that element.
Here's a little something:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us">
<head>
<title>Title</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
a:focus{
color:red
}
</style>
</head>
<body>
<a accesskey="x" target="new" href="#">Hello</a>
</body>
</html>
So I'm working on a Windows machine. Thusly, I expect the anchor to go red when I press Alt+x. Yet it is not. Is there something wrong with the code, or am I expecting the wrong thing to happen?
On Firefox, nothing happens. Google chrome opens a new tab (as it should, the target attribute say "new", which means open in new tab/window) and IE jumps to the anchor but does nothing further.
回答1:
Firefox's accesskey modifier is Shift+Alt.
回答2:
If you're determined to assign accesskeys (and there may be some good reasons to use this terribly implemented feature), you should take a look at WebAIM's page on accesskeys which outlines some of the obstacles you'll face.
One thing, on the Mac most browsers that support accesskeys --except Opera-- use Control not Command. Opera apparently uses Shift-Esc ... who knew?
回答3:
Alt + [the accesskey]
Internet Explorer for Windows
Chrome for Windows (not that Shift is required in some circumstances
Safari for Windows
Shift + Alt + [the accesskey]
Firefox for Windows
Ctrl + Option + [the accesskey]
Safari for Mac
Chrome for Mac
Firefox for Mac
回答4:
You have to press Enter to follow the link when it has the focus in IE.
回答5:
In firefox you need to press shift+alt. In IE hitting the modifyer key only sets focus to that element, this is by design. These are the only two browsers accessible with my screen reader so can't give you more info on others.
回答6:
If you are using firefox on mac then, you should press ctrl + 'your access key'.
来源:https://stackoverflow.com/questions/515951/html-attribute-accesskey-not-working-as-it-should