【问题标题】:find element by css property通过 css 属性查找元素
【发布时间】:2012-07-08 18:48:13
【问题描述】:

我有一个 div,其中包含大约 100 个其他 div 元素。每个div 元素都有topleft 属性。如何找到具有最大left 属性的div
我需要最好的表现。谢谢。

【问题讨论】:

标签: jquery


【解决方案1】:

试试这个,

Live Demo

var divWithTopLeft = null;
var maxLeft = 0;
$('div').each(function(){
    left = this.style.left.replace('px','');
    if(left > maxLeft )
    {
         maxLeft = left;
         divWithTopLeft = this;
    }   
});

【讨论】:

  • Jfyi,如果你在 fiddle 中定义了.class1 { position:relative; },那么你也可以使用this.offsetLeft 属性而不是this.style.left.replace('px',''); 来让代码运行得更快一点。
猜你喜欢
  • 2020-10-30
  • 2022-07-08
  • 2020-01-01
  • 1970-01-01
  • 2012-06-30
  • 2013-11-03
  • 2014-03-12
  • 1970-01-01
相关资源
最近更新 更多