a:不是[href ^ =" .."]("不以"运算符开始)不工作

时间:2017-09-05 14:47:37

标签: jquery

我有一堆A-links组织如下,

div class="thumbnail_wrap">
    <a href="photos/2014/1.png">..</a>
    <a href="photos/2014/2.png">..</a>
    <a href="photos/2015/1.png">..</a>
    <a href="photos/2016/1.png">..</a>
     ..
</div>

我需要选择与var year

中包含的特定年份相对应的所有链接
alert('Year selected: ' + year + ' ' + 
      'Photos in this section: ' + 
      $('.thumbnail_wrap > a[href^="photos/' + year + '"]').length + ' ' + 
      'Photos NOT in this section: ' + 
      $('.thumbnail_wrap > a:not[href^="photos/' + year + '"]').length   );

// Find all images with the "/photos/<YEAR>" URL and make them visible
$('.thumbnail_wrap > a[href^="photos/' + year + '"]').show();
// Find all images with the wrong Year URL and make them hidden
$('.thumbnail_wrap > a:not[href^="photos/' + year + '"]').hide();

第一个表达式始终有效。但是a:not表达式不起作用。得到此错误,

TypeError: a is undefined  jquery-1.10.2.js

1 个答案:

答案 0 :(得分:1)

$('.thumbnail_wrap > a:not([href^="photos/' + year + '"])').hide();

您需要在选择器

周围添加括号