【问题标题】:Height of div element returns zerodiv 元素的高度返回零
【发布时间】:2017-04-07 17:30:07
【问题描述】:

有谁知道为什么这里的高度可能为零?

    $(document).ready(function () {
                alert($("#hello").height());
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img id="hello" class="browseIMG" src="https://udemy-images.udemy.com/course/750x422/64132_926c_10.jpg" />

【问题讨论】:

  • 它给了我 422 警报...
  • 我已将代码放入 sn-p - 但具有讽刺意味的是,代码现在可以工作了。

标签: javascript html height


【解决方案1】:

这是因为即使在成功加载 DOM 之后,您甚至在从网络加载图像之前就获得了图像的高度。

可能你必须在图像的加载事件中这样做。

$('#hello').load(function() {  
  alert($("#hello").height());
});  

【讨论】:

    【解决方案2】:

    删除document.ready 函数,因为它在实际加载之前返回元素的高度。

    alert($("#hello").height());
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <img id="hello" class="browseIMG" src="https://udemy-images.udemy.com/course/750x422/64132_926c_10.jpg" />

    【讨论】:

      【解决方案3】:

      因为你的元素还没有加载,试试这个:

          $("#hello").load(function () {
                      alert($("#hello").height());
          });
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <img id="hello" class="browseIMG" src="https://udemy-images.udemy.com/course/750x422/64132_926c_10.jpg" />

      【讨论】:

        猜你喜欢
        • 2015-06-19
        • 2013-11-22
        • 2012-04-19
        • 2019-10-17
        • 1970-01-01
        • 1970-01-01
        • 2018-11-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多