获取jQuery集中最宽的元素 [英] Get widest element in a jQuery set
本文介绍了获取jQuery集中最宽的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一堆带有不同文字内容的< span>
元素。
Let's say I have a bunch of <span>
elements with different text content.
我怎样才能获得最宽的< span>
?
How I can I get the widest <span>
?
jQuery很好。我只关心识别跨度,而不是宽度本身的值。
jQuery is fine. I only care about identifying the span, not the value of the width itself.
类似的问题是这里。但他们只得到宽度的值。
A similar question is here. But they only get the value of the width.
以下是我的开始方式:
$('span').each(function(id, value){
if ($(this).width() > w) {
largestSpan = id;
}
});
推荐答案
var maxWidth = 0;
var widestSpan = null;
var $element;
$("span").each(function(){
$element = $(this);
if($element.width() > maxWidth){
maxWidth = $element.width();
widestSpan = $element;
}
});
这篇关于获取jQuery集中最宽的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文