【问题标题】:Element height not being returned for immediate child element没有为直接子元素返回元素高度
【发布时间】:2018-11-13 12:59:01
【问题描述】:

我有以下 HTML:

<div id="cookie-notice" class="banner-message with-icon">
    <div class="message-container">
        <div class="icon"><img src="/Assets/Media/Images/cookies.svg" alt="Cookies"></div>
        <div class="message">We use cookies for essential functionality and to track visits.</div>
    </div>
    <div class="button-container">
        <a class="md-flat-button" data-close-id="cookie-notice">Dismiss</a>
    </div>
</div>

因为它是一个横幅,所以它开始隐藏生命,在这种情况下,在横幅消息级别使用 height: 0;

目前我使用 jquery 返回message 跨度的高度,它有效。如果需要,我还可以获得icon 元素的高度。

但是,message-container 的高度返回 0。

我无法理解的是,为什么所有子元素都不返回 0,而不仅仅是直接子元素。

更重要的是,我该如何解决?

* 更新 *

我找到了导致问题的原因 - 但不幸的是还不知道如何解决它。

sn-p 展示了两个例子,第一个使用 flexbox 将两个盒子并排对齐,另一个没有 flexbox。

如您所见,一个返回容器的高度,另一个不返回。

设计规定 div 位于同一行,并且由于对齐方式大约有 8 种变化,因此 flexbox 仍然是更可取的解决方案。

$(document).ready(function () {

  $banner1 = $("#banner1").first();
  $container1 = $banner1.find(".message-container").first();
  $message1 = $container1.find(".message").first();
  var containerHeight1 = $container1.height();
  var messageHeight1 = $message1.height();

 $("#result1").html("Message height is: " + messageHeight1 + ", Container height is: " + containerHeight1 + " - flexbox");

  $banner2 = $("#banner2").first();
  $container2 = $banner2.find(".message-container").first();
  $message2 = $container2.find(".message").first();
  var containerHeight2 = $container2.height();
  var messageHeight2 = $message2.height();

 $("#result2").html("Message height is: " + messageHeight2 + ", Container height is: " + containerHeight2 + " - no flexbox");
});
.banner-message {
  height: 0;
  overflow:hidden;
  }

#banner1 {
  display: flex;
}
  
 .button-container a {
    padding: 0 16px;
 }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="banner1" class="banner-message">
  <div class="message-container">
    <div class="message">This is a bit of text that I need to get the height of</div>
  </div>
  <div class="button-container">
    <a>BUTTON</a>
  </div>
</div>
<div id="result1"></div>
<!-- Second test - this time with out height of zero -->
<div id="banner2" class="banner-message">
  <div class="message-container">
    <div class="message">This is a bit of text that I need to get the height of</div>
  </div>
  <div class="button-container">
    <a>BUTTON</a>
  </div>
</div>
<div id="result2"></div>

【问题讨论】:

  • 我真的不明白为什么应该关闭它 - 如果您认为应该关闭,请发布原因。
  • 为接近投票提供的“原因”是没有提供复制所需的所有代码。这里好像够了,或许你可以重新排列成一个sn-p?
  • "我不明白为什么所有子元素都不返回 0,而不仅仅是直接子元素。" - 它们并不完全相同子元素的类型。 1 是 block 元素,2 是 inlinediv 和 2x span)。尝试将它们全部更改为 div 或全部更改为 span,看看你得到了什么。
  • @freedomn-m - 将跨度更改为 div 不会影响任何结果,也不会将 div 更改为跨度。
  • 能否多分享一些CSS,可能会影响结果的获取方式

标签: javascript jquery html css flexbox


【解决方案1】:

这是因为 flexbox 和默认对齐行为 stretch。对于第一种情况,当您将height:0 设置为弹性容器时,子元素(弹性项目)的高度将拉伸以适应父高度;因此它也会有height:0

为了更好地看到这一点,请删除 overflow:hidden 并添加边框:

$(document).ready(function() {

  $banner1 = $("#banner1").first();
  $container1 = $banner1.find(".message-container").first();
  $message1 = $container1.find(".message").first();
  var containerHeight1 = $container1.height();
  var messageHeight1 = $message1.height();

  $("#result1").html("Message height is: " + messageHeight1 + ", Container height is: " + containerHeight1 + " - flexbox");

  $banner2 = $("#banner2").first();
  $container2 = $banner2.find(".message-container").first();
  $message2 = $container2.find(".message").first();
  var containerHeight2 = $container2.height();
  var messageHeight2 = $message2.height();

  $("#result2").html("Message height is: " + messageHeight2 + ", Container height is: " + containerHeight2 + " - no flexbox");
});
.banner-message {
  height: 0;
  margin-bottom:50px;
}

#banner1 {
  display: flex;
}

.message-container a {
  padding: 0 16px;
}

#banner1,
#banner2 {
  border: 1px solid red;
}
.message-container {
  border: 1px solid green;
}
#result1,#result2 {
 background:yellow;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="banner1" class="banner-message">
  <div class="message-container">
    <div class="message">This is a bit of text that I need to get the height of</div>
  </div>
  <div class="button-container">
    <a>BUTTON</a>
  </div>
</div>
<div id="result1"></div>
<!-- Second test - this time with out height of zero -->
<div id="banner2" class="banner-message">
  <div class="message-container">
    <div class="message">This is a bit of text that I need to get the height of</div>
  </div>
  <div class="button-container">
    <a>BUTTON</a>
  </div>
</div>
<div id="result2"></div>

如您所见,您正在计算的高度由绿色边框定义,在第一种情况下它是 0,但在第二种情况下不是。

为了避免这种情况,将对齐方式更改为不同于拉伸的方式:

$(document).ready(function() {

  $banner1 = $("#banner1").first();
  $container1 = $banner1.find(".message-container").first();
  $message1 = $container1.find(".message").first();
  var containerHeight1 = $container1.height();
  var messageHeight1 = $message1.height();

  $("#result1").html("Message height is: " + messageHeight1 + ", Container height is: " + containerHeight1 + " - flexbox");

  $banner2 = $("#banner2").first();
  $container2 = $banner2.find(".message-container").first();
  $message2 = $container2.find(".message").first();
  var containerHeight2 = $container2.height();
  var messageHeight2 = $message2.height();

  $("#result2").html("Message height is: " + messageHeight2 + ", Container height is: " + containerHeight2 + " - no flexbox");
});
.banner-message {
  height: 0;
  margin-bottom:50px;
}

#banner1 {
  display: flex;
  align-items:flex-start; /*added this*/
}

.message-container a {
  padding: 0 16px;
}

#banner1,
#banner2 {
  border: 1px solid red;
}
.message-container {
  border: 1px solid green;
}
#result1,#result2 {
 background:yellow;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="banner1" class="banner-message">
  <div class="message-container">
    <div class="message">This is a bit of text that I need to get the height of</div>
  </div>
  <div class="button-container">
    <a>BUTTON</a>
  </div>
</div>
<div id="result1"></div>
<!-- Second test - this time with out height of zero -->
<div id="banner2" class="banner-message">
  <div class="message-container">
    <div class="message">This is a bit of text that I need to get the height of</div>
  </div>
  <div class="button-container">
    <a>BUTTON</a>
  </div>
</div>
<div id="result2"></div>

【讨论】:

    猜你喜欢
    • 2015-05-01
    • 2019-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-02
    • 2017-08-26
    • 1970-01-01
    相关资源
    最近更新 更多