jQuery - 页面上最宽的项目

Jer*_*emy 2 jquery

如何使用jQuery在网页上找到最宽的项目(宽度设置为css或作为属性)?

Nik*_*iko 8

不会很快,但应该做的伎俩

var widest = null;
$("*").each(function() {
  if (widest == null)
    widest = $(this);
  else
  if ($(this).width() > widest.width())
    widest = $(this);
});
Run Code Online (Sandbox Code Playgroud)

这应该可以解决问题

这个版本可能会稍微快一点(但绝对不是那么clienat):

var widest = null;
// remember the width of the "widest" element - probably faster than calling .width()
var widestWidth = 0;
$("*").each(function() {
  if (widest == null)
  {
    widest = $(this);
    widestWidth = $(this).width();
  }
  else
  if ($(this).width() > widestWidth) {
    widest = $(this);
    widestWidth = $(this).width();
  }
});
Run Code Online (Sandbox Code Playgroud)

我还建议你限制你经历的节点类型(即使用div而不是*)