代码之家  ›  专栏  ›  技术社区  ›  Chris

如果有3个以上的元素具有相同的x位置,jquery会将类添加到所有元素中

  •  0
  • Chris  · 技术社区  · 7 年前

    我有一个多栏的网站。每列可以有0-10行。 这些行包含各种元素,但在每行中都有一个特定的元素位于相同的x位置。

    let xPositions = []
    let specialElements = $("span[aria-label='specific text']")
    specialElements.each(function() { 
        xPositions.push(this.x) // obviously this.x does not work. How can I get x?
    })
    specialElements.each(function() { 
        let count = xPositions.reduce((cnt, val) => val == this.x ? cnt + 1 : cnt)
        if(count > 3){
            this.addClass('highlight')
        }
    })
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   cssBlaster21895    7 年前

    我知道我可能很幼稚,但我理解x作为相对于父容器的位置,可能使用jquery位置函数:

    elPosition = $(this).position();
    xPositions.push(elPosition.left);