How to select all elements with particular ARIA value using jQuery?

你离开我真会死。 提交于 2019-12-19 05:17:14

问题


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



来源:https://stackoverflow.com/questions/23944253/how-to-select-all-elements-with-particular-aria-value-using-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!