显示展开/折叠部分的偏移坐标

时间:2014-01-14 16:47:45

标签: javascript jquery html5 sections

我的网页上有expand/collapse部分。展开/展开是使用section而不是div/table完成的。我的代码是:

<section id="examples">
    <text id = "ui-examples">
        <p class="the-data">Of course you can add other text before, after, and around the elements described in the previous section.</p>
    </text>
</section>

在加载页面时,会发生许多the-data的实例,因此ui-examples会加载不同的ID和相应的the-data。如何让这些不同的text ids计算每个offset的{​​{1}}坐标?

编辑格式

1 个答案:

答案 0 :(得分:0)

我会做这样的事情:

HTML

<section class="examples">
    <text class="ui-examples">
        <p class="the-data">Of course you can add other text before, after, and around the elements described in the previous section.</p>
    </text>
</section>

和jQuery

$(document).ready(function() {
    $('.examples').each(function() {
        var theData = $(this).find('.the-data');
        console.log(theData.offset().left);
        console.log(theData.offset().top);
    });
});

这是第一次等待文档准备就绪,一旦它然后遍历每个div,在该循环中有一个“example”类,我们使用find jQuery函数搜索所有的孩子当前div(在这种情况下,它是.example的一个实例)与选择器匹配。因此,通过使用.the-data,它将找到您要查找的段落标记。

然后,您可以使用offset()来获取坐标。

相关问题