Html 如何使用 JavaScript 将 `aria-disabled` 设置为 true

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17468639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 10:45:49  来源:igfitidea点击:

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

javascripthtmlcss

提问by Arup Rakshit

I have no idea about JS. But there is needed one line of code in my Ruby. I have the below html.

我对 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>

I want JS code to make the first and second button to make them visible. What would be the code?

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

Please help.

请帮忙。

回答by jQuery00

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

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);

Also button require close tag

按钮也需要关闭标签

回答by karaxuna

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

回答by Kaizo

As asked there is needed one line of code:

正如所问there is needed one line of code

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