jQuery在.html后获取元素偏移

问题描述

我执行以下操作以将html插入元素:

var html = '<div id="example-data">
    <div id="post1" class="post">1</div>
    <div id="post2" class="post">2</div>
    <div id="post3" class="post">3</div>
</div>';

$('#container-posts').html(html);

在真实的html中,.post元素中具有固定高度的图像,并且这些元素没有任何填充,边距或边框(但是这些元素的子元素却有)。

然后,在加载后,我需要从文档顶部获取元素的偏移量。

$('#post2').offset().top;

问题在于它会返回错误的元素高度值,并且还会返回错误的偏移量。

它在加载后使用click功能时有效。

例如,如果我将post类的元素的高度设置为固定高度,则它也可以工作:

var html = '<div id="example-data">
    <div id="post1" class="post" style="height: 500px;">1</div>
    <div id="post2" class="post" style="height: 500px;">2</div>
    <div id="post3" class="post" style="height: 500px;">3</div>
</div>';

这是正确的行为还是我做错了什么?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)