jQuery 获取前 3 li 标签的高度
我正在用动态内容填充无序列表,列表高度将适合内容,有谁知道如何获取无序列表中前 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这为您提供了它们的所有高度...但是您可以轻松地将所需的代码放入函数中以对每个列表项执行某些操作。
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.
http://jsfiddle.net/rnpAE/1/
EDIT: Shortened
$('li').nextUntil(':eq(2)')
to$('li:lt(3)')
如果您想单独计算它们,请使用 eq 选择器
http://api.jquery.com/eq-selector /
If you want to calculate them individually use the eq selector
http://api.jquery.com/eq-selector/
首先,我将 var 设置为负 1 项...
然后,对于每个项目,我添加 li 的高度...
然后我设置 ul 的scrollTop ...
有点老套,但最适合我
First I set a var to be minus 1 item...
Then, for each item, I add the height of the li ..
Then I set the ul's scrollTop ...
A little hacky, but works best for me
或者如果您想包含填充/边距,请使用
outerHeight
。Or use
outerHeight
if you want to include padding/margin.