【问题标题】:get the highest div from main div jquery从主 div jquery 中获取最高的 div
【发布时间】:2011-11-18 21:37:43
【问题描述】:

我也见过thisthis,但我就是无法让它们工作。

我有一个主 div,里面有许多具有不同 id 和类的孩子。 我需要找到一种方法来获取某个类的高度并根据它们设置下一个 div 的高度。 我知道它不起作用,但它可以帮助您了解我想要实现的目标:

var placesHeight = 0;
$('.places').each(function(){
    if(height > 35 ) {
    $(this).next('.nextdiv').css('height','(placesHeight+25)+"px"');
});

编辑:感谢大家的快速帮助。我尝试了所有这些并意识到我需要的是:

var placesHeight = $('.places').height();
$('.nextdiv').css('height', placesHeight+25);

【问题讨论】:

    标签: jquery height each


    【解决方案1】:

    我希望这会有所帮助,

    <script type="text/javascript" src="jquery-1.6.3.min.js"></script>
    <script>
    function test(){
        $("#my_div").children('.check_me').each(function(){
            alert( $(this).outerHeight());
        });
    }
    </script>
    <style>
    #my_div div{
        display:block;
        margin:5px;
        border:1pt solid #ccc;
    }
    </style>
    <button onclick='test()'>try me</button>
    
    <div id='my_div'>
        <div id='one_line' class='check_me'>
            first
        </div>
        <div id='two_lines'>
            first<br>second
        </div>
        <div id='three lines' class='check_me'>
            first<br>second<br>third
        </div>
    <div>
    

    【讨论】:

      【解决方案2】:

      这是你的问题:

      .css('height','(placesHeight+25)+"px"');
      

      placesHeight 的值不会被解析 - 它会尝试将高度的值设置为(placesHeight+25)+"px"。它可以更简单地完成:

      .css('height', placesHeight+25);
      

      “px”将自动为您添加。

      【讨论】:

      • 为什么不直接使用.height('yourheight') 呢?
      • 我试图模仿代码并修复“损坏”位 - 老实说,我无法遵循代码的意图。我希望语法问题的简单修复可以让@elbatron 找到所需的语义解决方案。
      【解决方案3】:

      查看更多标记会很有帮助 - 最好使用 jsfiddle。不过,这样的事情可能会奏效:

      $('.places').each(function(){
          var $this = $(this),
              height = $(this).height();
          height > 35 && $this.next().height(height);
      });
      

      【讨论】:

        【解决方案4】:

        试试这个

        $(this).next('.nextdiv').height(placesHeight+25);
        

        【讨论】:

          【解决方案5】:
          var placesHeight = 0;
          $('.places').each(function(){
              if($(this).height() > 35 ) {
              $(this).next('.nextdiv').css('height',$(this).height()+25+"px");
          });
          

          类似的东西?

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2022-11-14
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-10-18
            • 1970-01-01
            相关资源
            最近更新 更多