[英]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: 您可以使用简单的
querySelector
或querySelectorAll
来实现此目的,无需库也不需要任何循环:
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>
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>
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.