Obtenir l’élément le plus large dans un ensemble jQuery

Disons que j’ai un tas d’éléments avec un contenu de texte différent.

Comment puis-je obtenir le plus large ?

jQuery va bien. Je me soucie seulement d’identifier la scope, pas la valeur de la largeur elle-même.

Une question similaire est ici . Mais ils ne reçoivent que la valeur de la largeur.

Voici comment je commencerais:

 $('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; } }); 

Utilisation de jQuery pour identifier la plus grande étendue dans une div:

 var oSpan; // Container object for loop item var oWidest; // Container object for current widest in loop var nWidth = 0; // Int to store current span width var nWidest = 0; // Int to contain widest items width var aWidest = []; // Array to contain multiple matching spans // Call each function on spans within div $('#divContainer span').each(function(nIndex) { // Set reference to current span to avoid multiple calls per iteration oSpan = $(this); // Set current width to avoid multiple calls per iteration nSpanWidth = oSpan.width(); // Compare current width to widest width if (nSpanWidth == nWidest) { // If exact,add to array and set current widest items aWidest.push(oSpan); oWidest = oSpan; nWidest = nSpanWidth; } else if( nSpanWidth >= nWidest ) { // If wider, reset array and set current widest item oWidest = oSpan; nWidest = nSpanWidth; aWidest = [].push(oWidest) } else { } // Move along short stuff. });