Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery-页面上最宽的项_Jquery - Fatal编程技术网

jQuery-页面上最宽的项

jQuery-页面上最宽的项,jquery,Jquery,如何使用jQuery在网页上找到最宽的项目(css中设置的宽度或作为属性设置的宽度)?不会很快,但应该做到这一点 var widest = null; $("*").each(function() { if (widest == null) widest = $(this); else if ($(this).width() > widest.width()) widest = $(this); }); 这应该能奏效 var widest = null; $(

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

不会很快,但应该做到这一点

var widest = null;
$("*").each(function() {
  if (widest == null)
    widest = $(this);
  else
  if ($(this).width() > widest.width())
    widest = $(this);
});
这应该能奏效

var widest = null;
$("*").each(function() {
  if (widest == null)
    widest = $(this);
  else
  if ($(this).width() > widest.width())
    widest = $(this);
});
这个版本可能会稍微快一点(但clienat肯定不是这样):

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

回答得好Niko

我在它的基础上稍微做了一些改进,这样它只显示页面上可见的元素,因为有时它会返回设置了较大宽度但可能有显示的项目:例如,没有设置

var widest = null;

$("*").each(function() {
    if (widest === null && $(this).is(':visible') ) {
        widest = $(this);
    } else if ( $(this).is(':visible') && $(this).width() > widest.width() ) {
        widest = $(this);
    }
});

如果需要,可能需要检查outerWidth()。请注意DOM的长度。这个调用可能会很昂贵。有道理,我想我希望得到某种max函数。哇,最宽的东西,明确设置了宽度?那个宽度?正文文档窗口屏幕