在jQuery上获取最后一个li [英] Get last li on a line jQuery
问题描述
我们有一个简单的 ul
<ul>
<li>some text</li>
<li>some some text</li>
<li>text more</li>
<li>text here</li>
</ul>
ul { text-align: center; width: 100px; }
li { width: auto; display: inline; }
所以 ul
可以有几行。如何在 ul $ c $中占用最后
li
c>?
So the ul
can have several lines. How do I take last li
of the each line inside ul
?
推荐答案
如果在ul 的每一行上的 last li,那么你的意思是最后一个每个 ul
中 li
,然后:
If by last li on the each line of the ul you mean the last li
in each ul
, then:
$('ul li:last-child');
但是,如果你的意思是你有 li
在你的源代码中的几行上写了相同的 ul
,你现在想要获得每一行的最后一行,那么答案就是你不能。 DOM不关心代码中的换行符。
However, if you mean that you have your li
's within the same ul
written up on several lines in your source code, and you now want to get the last one on each line, then the answer is that you can't. The DOM does not care about your newline characters in your code.
注意:正确的方法是执行此操作给那些 li
'一个单独的类。
Note: the correct way to do this would be to give those li
's a separate class.
<ul>
<li></li><li></li><li class="last"></li>
<li></li><li></li><li class="last"></li>
<li></li><li></li><li class="last"></li>
</ul>
现在你可以使用CSS
Now you can use CSS
li.last { color: #444 }
和jQuery
$('li.last');
正确的方式......
the proper way...
这篇关于在jQuery上获取最后一个li的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!