javascript - div 元素的高度返回零

标签 javascript html height

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

    $(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" />

最佳答案

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

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

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

关于javascript - div 元素的高度返回零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43274384/

相关文章:

java - Spring MVC JSP Jquery 在按钮单击后重定向错误上调用 Controller 方法

python - 在原始 HTML 文件上写回 Beautifulsoup 时出错

javascript - jQuery 查找类并将其存储为变量

html - 为什么高度不是: 100% work to expand divs to the screen height?

javascript - jQuery:使用一个函数切换多个部分的高度

javascript - 使用 JQuery 动态更改 AwesomeIcon 的类

javascript - 我的谷歌地图不加载所有位置

javascript - Angular 位置搜索不适用于 &

javascript - 当最后一只猫显示完整图像时如何发出警报?

html - 将 div 扩展到页面高度减去像素的 100%