0
我有一系列在每个页面上重复10次的文章。该结构是像这样:使用jQuery查找一个元素相对于另一个元素的距离
<article class="postWrap">
<h2>Title</h2>
<p>Here is text</p>
</article>
我需要找到距离p标签是对文章的顶部。因此,根据标题的长度,p标签距文章顶部的距离可能会有所不同。最好的方法很可能是使用offset()
,但我无法正常工作。
感谢
UPDATE:
这里是工作的代码我写的,但我想有是绕了一个更好的办法:从你的问题假设
$(".postWrap").each(function(){
var postWrap = $(this).offset().top;
var firstP = $(this).find("p:first-of-type").offset().top;
var diff = firstP - postWrap;
var meta = $(this).find(".meta").css({'marginTop' : diff})
});