最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery targeting aria attribute containing value - Stack Overflow

programmeradmin0浏览0评论

In an HTML structure of :

<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-describedby containing my value ( for example four ), but leave all others untouched.

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 ??

In an HTML structure of :

<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-describedby containing my value ( for example four ), but leave all others untouched.

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 ??

Share Improve this question asked Feb 22, 2014 at 5:39 Obmerk KronenObmerk Kronen 16k17 gold badges70 silver badges107 bronze badges 8
  • because there is no attribute value starting with four – Arun P Johny Commented Feb 22, 2014 at 5:42
  • your second code is fine does not hide all elements - jsfiddle/arunpjohny/4cdak/1 – Arun P Johny Commented Feb 22, 2014 at 5:43
  • first one also is fine - jsfiddle/arunpjohny/4cdak/2 – Arun P Johny Commented Feb 22, 2014 at 5:44
  • isnt´s the * stands for containing ? ( sorry, I am really bad with anything that is not PHP ) – Obmerk Kronen Commented Feb 22, 2014 at 5:44
  • 1 Read Attribute Starts With Selector name^="value" – Tushar Gupta - curioustushar Commented Feb 22, 2014 at 5:46
 |  Show 3 more ments

1 Answer 1

Reset to default 5

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

You are hiding all div elements whose aria-describedby attribute 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 someclass use it like

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

Demo: Fiddle

发布评论

评论列表(0)

  1. 暂无评论