javascript jQuery 定位包含值的 aria 属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21950246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 22:04:50  来源:igfitidea点击:

jQuery targeting aria attribute containing value

javascriptjquery

提问by Obmerk Kronen

In an HTML structure of :

在 HTML 结构中:

<div class="someclass" tabindex="0" aria-describedby="my-specified-action my-one-name">
<div class="someclass" tabindex="1" aria-describedby="my-specified-action my-two-name">
<div class="someclass" tabindex="2" aria-describedby="my-specified-action my-three-name">
<div class="someclass" tabindex="3" aria-describedby="my-specified-action my-four-name">
<div class="someclass" tabindex="3" aria-describedby="my-specified-action my-five-name">

I need to hide all elements that has an attribute aria-describedbycontaining my value ( for example four), but leave all others untouched.

我需要隐藏具有aria-describedby包含我的值的属性的所有元素(例如four),但保持所有其他元素不变。

jQuery('div[aria-describedby*="four"]').hide()

of course, if I will do :

当然,如果我会这样做:

jQuery('div:not([aria-describedby*="four"])').hide()

it will hide ALL elements ( also the ones containing my target ..)

它将隐藏所有元素(还有包含我的目标的元素..)

for some reason, this is not working for me ,..

出于某种原因,这对我不起作用,..

jQuery('div:not([aria-describedby^="four"])').hide()

what am I doing wrong ??

我究竟做错了什么 ??

回答by Arun P Johny

Your problem is not the attribute selector, it is the target element selector.

您的问题不是属性选择器,而是目标元素选择器。

You are hiding all div elements whose aria-describedbyattribute does not contain four, instead you need to fine tune the selector to target only those element you want. In your case since all the div's share the class someclassuse it like

您正在隐藏其aria-describedby属性不包含的所有 div 元素four,而是需要微调选择器以仅针对您想要的那些元素。在你的情况下,因为所有的 div 共享类someclass使用它

jQuery('div.someclass:not([aria-describedby*="four"])').hide()

Demo: Fiddle

演示:小提琴