Calculate largest width of a set of elements using jQuery

后端 未结 7 1030
轻奢々
轻奢々 2020-12-16 03:07

I have made a quick Jsbin: http://jsbin.com/ujabew/edit#javascript,html,live

What i\'m trying to achieve is to find out which is the largest

7条回答
  •  天涯浪人
    2020-12-16 04:05

    This was my idea:

    $(document).ready(function () {
      var elements = $(".content ul li");
      var count = elements.length - 1;
      var width = [];
    
      elements.each(function (n) {
        width.push($(this).outerWidth(true));
        if (count == n) {
          width = Math.max.apply(Math, width);
        }
      });
    });
    

    I added the count of the number of elements and then runs the Math.max to get the largest number when each loop is done.

提交回复
热议问题