[英]How can I select the middle of three elements?
I have three <li>
tags in an <ul id="accordion">
. 我在<ul id="accordion">
有三个<li>
标签。 I now want to select the various items via jQuery. 我现在想通过jQuery选择各种项目。
To select the first <li>
item, we can use $("#accordion li:first")
. 要选择第一个<li>
项,我们可以使用$("#accordion li:first")
。
To select the last <li>
item, $("#accordion li:last")
works 要选择最后一个<li>
项, $("#accordion li:last")
有效
How can I select the middle <li>
element through a jQuery selector? 如何通过jQuery选择器选择中间的<li>
元素? There is no :middle
pseudo-class that I could use here. 没有:middle
我可以在这里使用的:middle
伪类。
You can combine two ":not" selectors so that you get all of the 'not first, and not last' elements like so: 你可以组合两个“:not”选择器,这样你就可以获得所有'not first,而不是last'元素:
HTML HTML
<ul>
<li>First</li>
<li>Second</li>
<li>Third</li>
</ul>
Javascript 使用Javascript
$('li:not(:first-child):not(:last-child)').css('color', 'red');
There is no :middle
selector, and this makes sense – what element would get matched when we have 4 items? 没有:middle
选择器,这是有道理的 - 当我们有4个项目时,哪个元素会匹配? However, you can access elements by their index. 但是,您可以按索引访问元素。 Using CSS Level 3 selectors, you can use the :nth-child(…)
pseudo-class: 使用CSS Level 3选择器,您可以使用:nth-child(…)
伪类:
#accordion li:nth-child(2)
The :nth-child
pseudoclass can also select repeating patterns of elements, such as all even or odd childs. :nth-child
pseudoclass也可以选择元素的重复模式,例如所有偶数或奇数:nth-child
元素。
jQuery additionally offers the nonstandard :eq(…)
pseudoclass for indexing, which you should probably use in this case: jQuery还提供了非标准的:eq(…)
伪类用于索引,在这种情况下你应该使用它:
$("#accordion li:eq(1)") // zero-based indexing
Further reading: 进一步阅读:
In this mode you can count number of elements. 在此模式下,您可以计算元素数量。
for example you have this : 例如,你有这个:
<ul id="someId">
<li>t1</li>
<li>t2</li>
<li>t1</li>
</ul>
the you can use length
to count lis 你可以用length
计算lis
var ln =$("#someId li").length;
after that find middle of length by this code 之后通过此代码找到中间长度
ln = ln / 2;
then use eq function of jquery to select middle Item like this 然后使用jquery的eq函数来选择这样的中间项
$("#someId li:eq("+ln+")").css("border","1px solid red");
But don't forget indexing by using eq starts from 0 . 但是不要忘记使用eq从0开始的索引。 I hope my answer help you :) 我希望我的回答可以帮到你:)
Another way to select second element is to use adjacent sibling selector +
: 选择第二个元素的另一种方法是使用相邻的兄弟选择器 +
:
$("#accordion li:first + li")
Here is a demo. 这是一个演示。
$("#accordion li:first + li").addClass('selected');
.selected {color: red;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul id="accordion"> <li>First</li> <li>Second</li> <li>Third</li> </ul>
And here is also generic solution for arbitrary number of li
elements (not only 3): 这里也是任意数量的li
元素的通用解决方案(不仅仅是3个):
$('#accordion li ~ li:not(:last)').addClass('selected');
.selected {color: red;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul id="accordion"> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li> <li>Item 4</li> <li>Item 5</li> </ul>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.