jQuery-页面上最宽的项目 [英] jQuery - widest item on page

查看:60
本文介绍了jQuery-页面上最宽的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I find the widest item (width set in css or as an attribute) on a web page using jQuery?

推荐答案

不会很快,但应该可以解决问题

wont be fast but should do the trick

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

这应该可以解决问题

此版本可能会稍快一些(但绝对不是这样):

this version might be slightly faster (but definitely not so 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();
  }
});

我还建议您限制通过的节点类型(即使用div代替*)

I also suggest you limit the type of nodes you go through (ie. use div instead of *)

这篇关于jQuery-页面上最宽的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆