버튼 role 이용하기

현재 번역은 완벽하지 않습니다. 한국어로 문서 번역에 동참해주세요.

button role은 사용자의 동작에 반응을 하는 클릭할 수 있는 요소에 사용되어야 합니다. 버튼만의 속성인 role="button"은 스크린 리더에서 버튼의 형태로 보여지는 어떤 요소(<p>, <span>, <div>)에도 적용될 수 있습니다. 추가적으로 role 속성은 토글 버튼을 만들기 위해 aria-pressed 속성과 함께 조합되어 사용될 수 있습니다.

노트: HTML 기본 버튼이 구버전의 유저 에이전트와 보조 기술에서 폭넓게 지원되기 때문에, 가능하면, 버튼 role 속성 보다 HTML의 기본 버튼<button>, <input type="button" /><input type="image" />)을 사용할 것을 권장합니다. 또한, HTML 버튼은 별도의 수정없이 기본적으로 키보드와 포커스 요구사항을 지원합니다.

키보드와 포커스

Buttons are interactive controls and thus focusable. If the button role is added to an element that is not focusable by itself (such as <span><div> or <p>) then, the tabindex attributes have to be used to make the button focusable.

Buttons can be operated by both mouse users as well as keyboard users. For native HTML <button> elements, the button's onclick event will fire both for mouse clicks and pressing of spacebar ,while the button has focus. But if another tag is used  to create a custom button, the onclick event will only fire when clicked by the mouse cursor, even if role="button" is used. Because of this, the developer will have to surely add a separate key event handler to the element so that the button can be triggered,even  when the space key is pressed.

Warning: Be careful when marking up links with the button role. Buttons are expected to be triggered using the Space key, while links are expected to be triggered using the Enter key. In other words, when links are used to behave like buttons, adding role="button" alone is not sufficient. It will also be necessary to add a key event handler that listens for the Space key in order to be consistent with native buttons.

버튼 토글

An advantage of using role="button" is that it allows the creation of toggle buttons. A toggle button can have two states: pressed and not pressed. Whether  a button is a toggle button or not can be indicated with the aria-pressed attribute in addition to the button role:

  • If aria-pressed is not used the button is not a toggle buttton.
  • If aria-pressed="false" is used the button is a toggle button that is currently not pressed. 
  • If aria-pressed="true" is used the button is a toggle button that is currently pressed.
  • if aria-pressed="mixed" is used, the button is considered to be partially pressed.

버튼 이름 붙이기

Buttons should always have an accessible name. For most buttons, this name will be the same as the text inside the button. In some cases, for example for icon buttons, the accessible name can be provided through an aria-label or aria-labelledby attribute.

유저 에이전트와 보조 기술에서 사용할 수 있는 효과들

When the button role is used, user agents should expose the element as a button control in the operating system's accessibility API. Screen readers should announce the element as a button and describe its accessible name. Speech recognition software should allow the button to be activated by saying "click" followed by the button's accessible name.

Note: Opinions may differ on how assistive technology should handle this technique. The information provided above is one of those opinions and therefore not normative.

예제

예제 1: 기본 버튼

In the snippet below, a span element has been given the button role. Because a <span> element is used, the tabindex attribute is required to make the button focusable and part of the tab order. Note that this snippet implies that CSS styles are provided to make the <span> element look like a button and that handleBtnClick and handleBtnKeyUp are event handlers that perform the button's action when clicked and when the Space key is pressed. 

<span role="button" tabindex="0" onclick="handleBtnClick()" onKeyUp="handleBtnKeyUp()">Save</span>

예제 2: 토글 버튼

In this snippet a native HTML button is converted to a toggle button using the aria-pressed attribute. Note that the tabindex attribute needs not to be used here because the <button> element is already focusable by default. When the button is activated, the aria-pressed value keeps on switching between true and false;

<!DOCTYPE HTML>
<html lang="en-US">
<head>
 <meta charset="UTF-8">
 <title>ARIA Button Role Example</title>
 <style type="text/css">
  [role="button"] {
   padding:3px;
   border: 1px solid #CCC;
  }
  [role="button"][aria-pressed="true"] {
   border: 2px solid #000;
  }
 </style>
 <script type="text/javascript">
  function handleBtnClick(event) {
    event = event || window.event;
    var pressed = event.target.getAttribute("aria-pressed") == "true";
    //change the aria-pressed value as the button is toggled:
    event.target.setAttribute("aria-pressed", pressed ? "false" : "true");
    //... (perform the button's logic here)
  }

  function handleBtnKeyUp(event) {
    event = event || window.event;
    if (event.keyCode === 32) { // check for Space key
      handleBtnClick(event);
    }
  }
 </script>
</head>
<body>
 <button role="button" aria-pressed="false" onclick="handleBtnClick(event)" onKeyUp="handleBtnKeyUp(event)">Edit Mode</button>
</body>
</html>

작업 예:

노트 

사용된 ARIA 속성

관련 ARIA 기법들

적합성

TBD: Add support information for common UA and AT product combinations

추가 자료

문서 태그 및 공헌자

 이 페이지의 공헌자: naduhy2
 최종 변경: naduhy2,