Javascript 针对包含值的属性的jQuery
在以下HTML结构中:Javascript 针对包含值的属性的jQuery,javascript,jquery,Javascript,Jquery,在以下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-descri
<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">
当然,如果我愿意:
jQuery('div:not([aria-describedby*="four"])').hide()
它将隐藏所有元素(也包括包含我的目标..)
出于某种原因,这对我来说不起作用
jQuery('div:not([aria-describedby^="four"])').hide()
我做错了什么???您的问题不是属性选择器,而是目标元素选择器 您正在隐藏其
aria descripeby
属性不包含four
的所有div元素,相反,您需要微调选择器以仅针对您想要的那些元素。在您的情况下,因为所有div共享类someclass
,所以请像这样使用它
jQuery('div.someclass:not([aria-describedby*="four"])').hide()
演示:因为没有以
four开头的属性值,所以第二个代码很好并没有隐藏所有元素-第一个也很好-是不是*
代表包含?(对不起,我对任何不是PHP的东西都很不了解)阅读谢谢!我想要这个。但它现在可以工作了…你能简单地解释一下顶部的附加代码吗?为什么需要它@ObmerkKronen您的代码jQuery('div:not([aria descripeby*=“four”])。hide()
表示隐藏所有div
元素,但不包含aria descripeby
属性。现在您有了一个container div,它没有aria descripbeby
属性,因此它为条件返回true,从而得到hidden@ObmerkKronen你能告诉我你想要哪种解释吗?我指的是代码return$(this.attr('aria-descripeby')
。对不起,我只是想学习和理解更多
jQuery('div.someclass:not([aria-describedby*="four"])').hide()