高度在自调用函数内部不起作用

时间:2016-02-18 07:18:14

标签: javascript jquery

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

是可以的



(function($){
	var clientHeight = document.getElementById('home').clientHeight;
	alert(clientHeight);
})(jQuery);

<div id="home" style="background-position: 50% 0px; height: 635px;">
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

  

在您的网页中加入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>