jQuery 获取前 3 li 标签的高度

发布于 2024-11-10 04:16:14 字数 1048 浏览 4 评论 0原文

我正在用动态内容填充无序列表,列表高度将适合内容,有谁知道如何获取无序列表中前 3 li 标签的高度?

生成的动态内容可能如下所示,所以我只想能够计算前 3 里标签的高度。

<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。

I am filling a unordered list with dynamic content and the list height will fir the content, does anyone know how I can get the height of the first 3 li tags in the unordered list?

The dynamic content produced might be something like below so I just want to be able to calculate the height of the first 3 li tags.

<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>

Thanks for any help
J.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

给妤﹃绝世温柔 2024-11-17 04:16:14

这为您提供了它们的所有高度...但是您可以轻松地将所需的代码放入函数中以对每个列表项执行某些操作。

var sum = 0;

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

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

编辑: 缩短$('li').nextUntil(':eq(2)')$('li:lt(3)')

This provides you with all of their height... but you could easily just put the code you want inside the function to perform something on each list item.

var sum = 0;

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

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

EDIT: Shortened$('li').nextUntil(':eq(2)') to $('li:lt(3)')

停顿的约定 2024-11-17 04:16:14

如果您想单独计算它们,请使用 eq 选择器

http://api.jquery.com/eq-selector /

var liHeight = $('li:eq(0)').outerHeight(); // Obtains height of first li

If you want to calculate them individually use the eq selector

http://api.jquery.com/eq-selector/

var liHeight = $('li:eq(0)').outerHeight(); // Obtains height of first li
猫卆 2024-11-17 04:16:14

首先,我将 var 设置为负 1 项...

var liTotalHeight = -24;

然后,对于每个项目,我添加 li 的高度...

liTotalHeight = liTotalHeight + $(this).outerHeight();

然后我设置 ul 的scrollTop ...

$(this).parent().scrollTop(liTotalHeight);

有点老套,但最适合我

First I set a var to be minus 1 item...

var liTotalHeight = -24;

Then, for each item, I add the height of the li ..

liTotalHeight = liTotalHeight + $(this).outerHeight();

Then I set the ul's scrollTop ...

$(this).parent().scrollTop(liTotalHeight);

A little hacky, but works best for me

━╋う一瞬間旳綻放 2024-11-17 04:16:14
$('li').height(); // First
$('li').next().height(); // Second
$('li').next().next().height(); // Third

或者如果您想包含填充/边距,请使用 outerHeight

$('li').height(); // First
$('li').next().height(); // Second
$('li').next().next().height(); // Third

Or use outerHeight if you want to include padding/margin.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文