首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JavaScript ARIA角色等效于nodeName

JavaScript ARIA角色等效于nodeName
EN

Stack Overflow用户
提问于 2022-08-05 07:05:37
回答 1查看 14关注 0票数 0

如果我想为我可以使用的所有HTML元素获取nodeName

代码语言:javascript
复制
nodes =       document.querySelectorAll("ins");
        for (let n = 0; n < nodes.length; n++) {
          nodes[n].setAttribute("data-name-prohibited", nodes[n].nodeName);
        }

这将返回数据-名称-禁止=“ins”。

我想得到ARIA角色类型

代码语言:javascript
复制
nodes = document.querySelectorAll("[role=insertion]");
        for (let n = 0; n < nodes.length; n++) {
          nodes[n].setAttribute("data-name-prohibited", nodes[n].roleName);
        }

roleName只是一个例子。我想要的是数据-名字-禁止=“插入”

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-08-05 07:12:33

您可以获取role属性值并将其传递给data-name-prohibited属性。

代码语言:javascript
复制
const nodes = document.querySelectorAll("[role=insertion]");
for (let n = 0; n < nodes.length; n++) {
  const currentNode = nodes[n];
  const role = currentNode.getAttribute('role');
  currentNode.setAttribute("data-name-prohibited", role);
}

但是如果您使用[role=insertion]查询,那么您已经知道了角色值,所以您可以简单地对其进行硬编码。

代码语言:javascript
复制
const role = 'insertion';
const nodes = document.querySelectorAll(`[role=${role}]`);
for (let n = 0; n < nodes.length; n++) {
  nodes[n].setAttribute("data-name-prohibited", role);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73245912

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档