简体   繁体   English

如何从具有相同属性的一堆元素中获得具有“ aria-hidden”真值的元素?

[英]How do I get an element with `aria-hidden` true out of a bunch of elements that have the same attribute?

I'm trying to loop through a list of elements that all have aria-hidden set to either false or true, in my case, only one element has it set to true, so I need to know which one is currently visible. 我试图遍历所有都将aria-hidden设置为false或true的元素列表,就我而言,只有一个元素将其设置为true,因此我需要知道当前可见的元素。

Markup: 标记:

<div id="screen-1" class="setup-step-screen" aria-hidden="false"></div>
<div id="screen-2" class="setup-step-screen" aria-hidden="true"></div>
<div id="screen-3" class="setup-step-screen" aria-hidden="true"></div>

As such, I'd like to retrieve screen-1 's id: 因此,我想检索screen-1的ID:

var current_step = $('.setup-step-screen').each( function(i, obj) {
    if( $(obj).attr('aria-hidden') === 'false') {
        return $(obj).attr('id');
    }
});
console.log( current_step);

Problem is, this returns: 问题是,这返回:

a.fn.init(2) [div#screen-1.setup-step-screen, div#screen-2.setup-step-screen, selector: ".setup-step-screen", prevObject: n.fn.init(1), context: document]

This is because it doesn't know when to stop the each , I believe, as such, this piece of code works: 这是因为我不知道何时停止each代码,因此,这段代码可以正常工作:

var current_step;
$('.setup-step-screen').each( function(i, obj) {
    if( $(obj).attr('aria-hidden') === 'false') {
        current_step = $(obj).attr('id');
    }
});

console.log(current_step);

But I'd like to make the assignment within the each . 但是我想在each任务中进行分配。

How do I fix this? 我该如何解决?

You can achieve this with a simple querySelector or querySelectorAll , no library nor any loops required: 您可以使用简单的querySelectorquerySelectorAll来实现此目的,无需库也不需要任何循环:

 console.log( document.querySelector('[aria-hidden="false"]').id ); console.log( Array.from( document.querySelectorAll('[aria-hidden="true"]'), ({ id }) => id ) ); 
 <div id="screen-1" class="setup-step-screen" aria-hidden="false"></div> <div id="screen-2" class="setup-step-screen" aria-hidden="true"></div> <div id="screen-3" class="setup-step-screen" aria-hidden="true"></div> 

Using vanilla JavaScript 使用原始JavaScript

You can use a CSS selector inside the method .querySelector . 您可以在.querySelector方法内使用CSS选择器。 In your case 就你而言

document.querySelectorAll('div[aria-hidden="false"]')

Will return all div elements that have aria-hidden set to false . 将返回所有将aria-hidden设置为false div元素。

Here is the code: 这是代码:

 const selection = document.querySelectorAll('div[aria-hidden="false"]') console.log(selection) 
 <div id="screen-1" class="setup-step-screen" aria-hidden="false"></div> <div id="screen-2" class="setup-step-screen" aria-hidden="true"></div> <div id="screen-3" class="setup-step-screen" aria-hidden="true"></div> 


Using jQuery 使用jQuery

If you want to use jQuery, the same can be applied: 如果要使用jQuery,则可以使用相同的方法:

 var current_step; $('div[aria-hidden="false"]').each( function(i, obj) { current_step = $(obj).attr('id'); }); console.log(current_step); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="screen-1" class="setup-step-screen" aria-hidden="false"></div> <div id="screen-2" class="setup-step-screen" aria-hidden="true"></div> <div id="screen-3" class="setup-step-screen" aria-hidden="true"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 屏幕阅读器是否可以读取具有 aria-hidden 和 css visible:hidden 属性的元素? - Does an elements with the attribute aria-hidden and css visibility:hidden get read by screen readers? 在量角器中,如何检查 aria-hidden = &quot;true&quot; 或 &quot;false&quot; - In Protractor, how to check aria-hidden = "true" or "false" 如何让 JAWS 更新其 aria-hidden 值缓存 - How to get JAWS to update its cache of aria-hidden values aria-hidden = true是否意味着您不必使用display:none? - Does aria-hidden=true mean you don't have to use display:none? 可访问性:`tabindex=&quot;-1&quot;` 是否意味着该元素对屏幕阅读器不可见(类似于 `aria-hidden=&quot;true&quot;`) - Accessibility: does `tabindex="-1"` mean the element is not visible to screenreaders (similar to `aria-hidden="true"`) 显示模式时的“aria-hidden 元素不包含可聚焦元素”问题 - “aria-hidden elements do not contain focusable elements” issue when modal is shown 从源代码中删除所有aria-hidden =“ true” - remove all aria-hidden=“true” from source code 无法将aria-hidden属性用作javascript对象 - Unable to use aria-hidden attribute as javascript object 如何添加 aria-hidden 属性以跨越焦点变化? - How to add aria-hidden property to span on focus change? Javascript 出现 aria-hidden 弹出窗口的问题 - Javascript Issue with aria-hidden popup
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM