高度在自调用函数中不起作用

height not working inside self invoking function

本文关键字:不起作用 函数 调用 高度      更新时间:2023-09-26

高度在自调用函数中不起作用,但在内部(文档).ready(function()

(function($){
	var clientHeight = document.getElementById('home').clientHeight;
	alert(clientHeight);
})(jQuery);
<div id="home" style="background-position: 50% 0px; height: 635px;">
</div>

在页面中包含jQuery库,否则您将在控制台中发现jQuery is not defined错误。

试试这个:

(function($) {
  var clientHeight = document.getElementById('home').clientHeight;
  alert(clientHeight);
})(jQuery);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="home" style="background-position: 50% 0px; height: 635px;">
</div>

或者只是删除document.ready,因为您没有使用任何其他jQuery方法

(function() {
  var clientHeight = document.getElementById('home').clientHeight;
  alert(clientHeight);
})();
<div id="home" style="background-position: 50% 0px; height: 635px;">
</div>