如何使用JavaScript将“aria-disabled”设置为true

How to set `aria-disabled` as true using JavaScript

本文关键字:设置 true aria-disabled 何使用 JavaScript      更新时间:2023-09-26

我对JS一无所知。但是在我的Ruby中需要一行代码。我有下面的html

<div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix">
   <div class="ui-dialog-buttonset">
      <button class="otherButtonClass ui-state-hover ui-state-focus" type="button" role="button" aria-disabled="false">
      <button class="otherButtonClass" type="button" role="button" aria-disabled="false" style="display: none;">
      <button class="cancelButtonClass" type="button" role="button" aria-disabled="false">
   </div>
</div>

我希望JS代码使第一个和第二个按钮可见。代码是什么?

请帮忙。

http://jsfiddle.net/SQ7SH/1/

var buttons = document.querySelectorAll('.ui-dialog-buttonset button');
    buttons[0].setAttribute('aria-disabled', true);
    buttons[1].setAttribute('aria-disabled', true);

此外,按钮需要关闭标签

当前设置aria-属性的方式是直接引用属性。

获取:

let el = document.getElementById('foobar');
console.log(el.ariaDisabled); // Should log the current value of aria-disabled.

设置:

let el = document.getElementById('foobar');
el.ariaDisabled = 'true';
console.log(el.ariaDisabled); // Should log 'true'.

参考:Element.ariaDisabled MDN

var buttons = document.getElementsByClassName('otherButtonClass');
for(var i = 0; i < buttons.length; i++){
    buttons[i].setAttribute('aria-disabled', 'true');
}

按要求there is needed one line of code:

document.querySelectorAll('.ui-dialog-buttonset .otherButtonClass').forEach(function (item) {item.setAttribute('aria-disabled', true);});