jQuery偏移方法是数组中所有元素的方法,而不仅仅是第一个元素



我的页面上有很少的divs。我想使用jQuery偏移方法找到所有这些divs的偏移。

$('div')。offset()

向我返回第一个div。

的偏移

但是,如果我想偏移所有的divs,是否有某种方法可以做到?

您必须在Divs上迭代:

$('div').each(function() {
     console.log($(this).offset());
});

在 @dystroy的答案上构建:

var offsetCollection = new Array();
$('div').each(function() {
     offsetCollection.push(
       $(this).offset(), $(this).index()
     );
});

您可以:

offsetCollection[0][0]; //div 1, offset
offsetCollection[0][1]; //div 1, elementIndex

最新更新