【问题标题】:Jquery offset method for all elements in an array rather than just the first one数组中所有元素的jQuery偏移方法,而不仅仅是第一个元素
【发布时间】:2012-10-08 15:53:36
【问题描述】:

我在同一个类的页面上几乎没有 div。我想使用 jquery offset 方法来查找所有这些 div 的偏移量。

$('div').offset()

返回第一个 div 的偏移量。

但是,如果我想要所有 div 的偏移量,有什么方法可以做到吗?

【问题讨论】:

    标签: javascript jquery html offset


    【解决方案1】:

    以@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
    

    【讨论】:

      【解决方案2】:

      您必须在 div 上进行迭代:

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

      【讨论】:

        猜你喜欢
        • 2012-07-11
        • 2016-07-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-30
        • 2015-03-21
        相关资源
        最近更新 更多