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)\')
    ,如果要单独计算它们,请使用eq选择器 http://api.jquery.com/eq-selector/
var liHeight = $(\'li:eq(0)\').outerHeight(); // Obtains height of first li
    ,首先,我将一个变数设为负1项...
var liTotalHeight = -24;
然后,对于每个项目,我添加li ..的高度。
liTotalHeight = liTotalHeight + $(this).outerHeight();
然后我设置ul的scrollTop ...
$(this).parent().scrollTop(liTotalHeight);
有点hacky,但最适合我     ,
$(\'li\').height(); // First
$(\'li\').next().height(); // Second
$(\'li\').next().next().height(); // Third
如果要包括填充/边距,请使用
outerHeight
。     

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...