简体   繁体   English

如何选择三个元素的中间值?

[英]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');

Also, check the JsFiddle 另外,检查JsFiddle

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.

相关问题 如何将页脚元素放在页脚中间? - How can I get my footer-elements in middle of the footer? 我怎么能有<div>布局分成三列,中间列有几行? - How can I have a <div> layout split into three columns with rows in the middle column? 如何在流体宽度容器中的左侧,中间和右侧的同一行中放置三个按钮? - How can I place three buttons in the same row at the left, middle and right in a fluid-width container? 如何以中间块始终为全角的方式将三块内容放在一行中? - How can I place three block of contents in one line in a way that the middle block is always full width? 如何在 bootstrap 4 中构建卡片标题以保持三个元素连续,截断中间元素中的文本以获得较小的屏幕尺寸? - How do I structure a card header in bootstrap 4 to keep three elements in a row, truncating the text in the middle element for smaller screen sizes? 如何在同一行中对齐图例标签中的三个元素? - How can I align three elements in a legend tag in the same line? 如何获得三个可以水平对齐的元素? - How can I get there three elements to align horizontally? 如何在轮播中每页显示三个元素? - How can I show three elements per page in a carousel? 如何使用 CSS 仅选择前三个元素 - How to select only first three elements with CSS 如何选择没有类别的元素? - How can I select elements that have no class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM