如果我想为我可以使用的所有HTML元素获取nodeName
nodes = document.querySelectorAll("ins");
for (let n = 0; n < nodes.length; n++) {
nodes[n].setAttribute("data-name-prohibited", nodes[n].nodeName);
}这将返回数据-名称-禁止=“ins”。
我想得到ARIA角色类型
nodes = document.querySelectorAll("[role=insertion]");
for (let n = 0; n < nodes.length; n++) {
nodes[n].setAttribute("data-name-prohibited", nodes[n].roleName);
}roleName只是一个例子。我想要的是数据-名字-禁止=“插入”
发布于 2022-08-05 07:12:33
您可以获取role属性值并将其传递给data-name-prohibited属性。
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]查询,那么您已经知道了角色值,所以您可以简单地对其进行硬编码。
const role = 'insertion';
const nodes = document.querySelectorAll(`[role=${role}]`);
for (let n = 0; n < nodes.length; n++) {
nodes[n].setAttribute("data-name-prohibited", role);
}https://stackoverflow.com/questions/73245912
复制相似问题