Javascript 函数来确定高度

Javascript 函数来确定高度,javascript,function,height,Javascript,Function,Height,简单问题 具有以下功能: var maxHeight = 0; function calcHeight(el){ el.each(function(){ if(maxHeight < el.find('.products-title').outerHeight(true)){ maxHeight = el.find('.products-title').height(); } }); el.find('

简单问题

具有以下功能:

var maxHeight = 0;

function calcHeight(el){

    el.each(function(){
        if(maxHeight < el.find('.products-title').outerHeight(true)){
            maxHeight = el.find('.products-title').height();
        }

    });

    el.find('.products-title').css('height',maxHeight);
}

calcHeight($('.mini-products-list > li'));
但在最大高度处未找到记录,但此功能有效:

$('.mini-products-list > li').each(function(){


    if(maxHeight < $(this).find('.products-title').outerHeight(true)){
        maxHeight = $(this).find('.products-title').height();

    }
});

$('.mini-products-list > li').find('.products-title').css('height',maxHeight);
问题是为什么会发生这种情况,以及使用该函数时哪个记录是正确的?

您没有使用该函数来引用函数中的当前元素。这里您使用el表示,所以每次它都将确定第一个元素

在那里也可以像贝娄一样使用这个

el.each(function(){
      if(maxHeight < $(this).find('.products-title').outerHeight(true)){
          maxHeight = $(this).find('.products-title').height();
      }
 });

为什么你不在那个函数中使用这个,你在那里使用el,这可能是原因。如果你在使用jQuery,请在你的问题中添加标记。这看起来像你在使用jQuery。它当然不仅仅是纯javascript。请为您正在使用的库添加标记。此外,您还应该发布一个HTML示例,最好是一个fiddle,例如jsfiddle.net,以显示问题。我认为@j08691已经有了它:el.find“.products-title”将只引用第一个元素的高度。您可以在每个循环中使用$this而不是èl`。