jquery选择所有<a> beside some special </a> <a>

时间:2015-11-25 07:37:21

标签: jquery jquery-selectors

I am sorry for the title, don't know how to describe it better.

I have following structure:

<div id="content">
  <ul id="linklist">
     <li><a>...</a></li>
     <li><a>...</a></li>
     <li><a>...</a></li>
     <li><a>...</a></li>
  </ul>
  <a>...</a>
  <div> //variable depth of divs
    <a>...</a>
  </div>
</div>

I want to select all links () beside the third and fourth one in the #linklist.

I've tried some jQuery selectors but i can't find a working one.

$(":not(#linklist li:gt(2) a) a")

does show the third and fourth link too. I think because the li are still exist in the match of "not".

What is the correct selector for the problem?

5 个答案:

答案 0 :(得分:1)

我采用不同的方法。 提供您不想选择唯一类名的元素(即.ignore

     <li><a>...</a></li>
     <li><a>...</a></li>
     <li><a class="ignore-me">...</a></li>
     <li><a class="ignore-me">...</a></li>

然后你可以这样做:

a:not(.ignore-me)

这将更易于维护(即,如果链接位置发生变化),并且同样适用于jQuery和CSS。

希望这有帮助。

答案 1 :(得分:1)

:gt选择器中的语法有点偏差。 :gt应位于#linklist a元素上,索引从零开始,因此应为1。试试这个:

$('a:not(#linklist a:gt(1))')

Example fiddle

答案 2 :(得分:0)

您可以将.not()方法与要从选择中删除的两个索引一起使用:

$('#linklist').find('a').not(':eq(2)').not(':eq(3)');

答案 3 :(得分:0)

尝试:

$('#linklist li').not('li:nth-child(3)').not('li:nth-child(4)');

或:

$('#linklist li').not('li:gt(1)');

请参阅:https://jsfiddle.net/eadp1jL5/1/

答案 4 :(得分:0)

试试这个:

$('#linklist li').not(":eq(3)").not(":eq(2)")