How to select all elements with particular ARIA va

2020-03-09 07:12发布

问题:

Given i have a sample page that looks like this:

<!DOCTYPE html>
<html>
<body>

<h1 aria-controls="name1">heading</h1>

<p aria-controls="name2">paragraph</p>

<span aria-controls="name1">span</span>

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</body>
</html>

How would i use jQuery to select the (2) elements with their aria-controls attribute set to name1? (ignoring the fact that the element types are different).

Thank you!

回答1:

The attribute selector

[aria-controls="name1"]

should work.

Docs: http://api.jquery.com/attribute-equals-selector/



回答2:

Use something like this -

WORKING DEMO

var elements = $("body").find("[aria-controls='name1']");

Above is for if you want to look for elements within a container eg body in this case, it can be some div also.

--OR--

var elements = $("[aria-controls='name1']"); 

Above is for if you want to get all the elements with this attribute