帮助使用 JQuery 选择器 :not(:last)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5640968/
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-08-26 19:33:15  来源:igfitidea点击:

Help with JQuery selector :not(:last)

jqueryjquery-selectors

提问by redconservatory

I'm trying to select all the bullets except the last bullet but I am doing something incorrectly.

我试图选择除最后一个项目符号之外的所有项目符号,但我做错了一些事情。

Here is my html

这是我的 html

<ul>
<li>Text</li>
<li>Text</li>
<li>Text</li>
<li>Text</li>
<li>Text</li>
</ul>

<ul>
<li>Text</li>
<li>Text</li>
<li>Text</li>
<li>Text</li>
<li>Text</li>
</ul>


<ul>
<li>Text</li>
<li>Text</li>
<li>Text</li>
<li>Text</li>
<li>Text</li>
</ul>

And my jQuery:

还有我的 jQuery:

jQuery('ul li:not(:last)').append(" | ");

It's adding the pipe to every bullet however...

然而,它正在为每个子弹添加管道......

回答by Matt Ball

Because your html contains multiple unordered lists, $('ul li')will selects every single <li>, in all of the <ul>s. :lastthen selects the final element in that list. Use :last-child, which gets the last <li>in eachof the <ul>s.

因为您的 html 包含多个无序列表,所以$('ul li')将选择<li>所有<ul>s 中的每一个, 。:last然后选择该列表中的最后一个元素。使用:last-child,它得到最后<li>每个中的<ul>秒。

$('ul li:not(:last-child)').append(' | ');

http://jsfiddle.net/mattball/qmVdb/1/

http://jsfiddle.net/mattball/qmVdb/1/