javascript – jQuery获得前3个li标签的高度

前端之家收集整理的这篇文章主要介绍了javascript – jQuery获得前3个li标签的高度前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在填充一个包含动态内容的无序列表,列表高度将填充内容,是否有人知道如何在无序列表中获得前3个li标签的高度?

产生的动态内容可能如下所示,所以我只想计算前3个li标签的高度.

<ul>
<li>23 Feb 2011<br />Synergy Launch new website...<br />Lorem ipsum dolor sit amet,consectetur adipiscing elit. Nunc gravida lacus a ligula dictum dignissim....</li>
<li>23 Feb 2011<br />Expat children "receive improv...<br />Expat children enjoy a better standard of education whilst living abroad compared to their home country according to the HSBC Offshore Offspring Report,...</li>
<li>25 Feb 2011<br />London Market favours Landlord...<br />The lettings market has swung dramatically in favour of landlords as an average six applicants chase every available property in London. This is a dramatic rise...</li>
<li>23 Feb 2011<br />Synergy Launch new website...<br />Lorem ipsum dolor sit amet,consectetur adipiscing elit. Nunc gravida lacus a ligula dictum dignissim....</li>
</ul>

谢谢你的帮助
J.

解决方法

这为您提供了所有高度……但您可以轻松地将所需的代码放在函数中,以便在每个列表项上执行某些操作.
var sum = 0;

$('li:lt(3)').each(function() {
   sum += $(this).height();
});

http://jsfiddle.net/rnpAE/1/

编辑:缩短$(‘li’).nextUntil(‘:eq(2)’)到$(‘li:lt(3)’)

猜你在找的jQuery相关文章