我的网页上有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}}坐标?
编辑格式
答案 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()
来获取坐标。