Skip to content

Instantly share code, notes, and snippets.

@YusukeHirao
Created February 10, 2014 07:59
Show Gist options
  • Save YusukeHirao/8912015 to your computer and use it in GitHub Desktop.
Save YusukeHirao/8912015 to your computer and use it in GitHub Desktop.
WAI ARIAに準じたインターフェイスを実装するためのjQueryプラグイン。 「role=button」編
$.fn.roleButton = function () {
return this.each(function (method) {
var $this,
isPressed;
function updatePressed () {
$this.attr('area-pressed', isPressed);
}
function changePressed () {
isPressed = !isPressed;
updatePressed();
}
$this = $(this);
if ($this.data('is-role-button-instance')) {
isPressed = $this.attr('area-pressed') === 'true';
$this.data('is-role-button-instance', true);
switch (method) {
default:
return isPressed;
}
} else {
$this.attr('role', 'button');
if (!$this.attr('tabindex')) {
$this.attr('tabindex', 0);
}
isPressed = $this.attr('area-pressed') === 'true';
$this.on('click keyup', function () {
$this.trigger('press', isPressed);
return;
});
$this.on('press', function () {
changePressed();
return true;
});
updatePressed();
}
});
};
var $buttons = $('button, input[type="button"], input[type="submit"], input[type="reset"], [role="button"]');
$buttons.roleButton();
$buttons.on('press', function () {
var $this = $(this);
var isPressed = $this.roleButton();
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment