我试图通过引用类名称来设置元素的最小高度,但返回错误: div div: cannot read property 'css‘of null div。
HTML元素
<div class="container contentContainer" id="topContainer">
<div class="row">
<div class="col-md-6 col-md-offset-3" id="topRow">
<p class="bold">Please scroll down for more information </p>
</div>
</div>
</div>
Javascript
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js"></script>
<script>
$(function () {
var x = document.getElementsByClassName("contentContainer");
alert(x);
if(x != null)
{
$(".contentContainer").css('min-height',$(window).height());
}
});
</script>
关于为什么我会得到这个错误,有什么建议吗?请让我知道。
当我包含外部javascript文件时出现问题:
<!-- <script type="text/javascript" src="js/monthly.js"></script> -->
发布于 2015-01-11 22:20:33
您同时加载了jQuery和Bootstrap,它们都占用了$
符号;要只使用jQuery,您可以使用规范名称:
jQuery(".contentContainer")
.css('min-height', jQuery(window).height());
或者,使用包装器:
jQuery(function($) {
$(".contentContainer").css('min-height',$(window).height());
});
发布于 2015-01-11 22:05:49
这是jQuery语法。
$(window).height();
您需要包含jQuery或使用原生JavaScript版本:
window.innerHeight;
为了使用jQuery,您需要包含jQuery library
发布于 2015-01-11 22:19:24
因为没有包含jQuery,所以在JSFiddle上会出现错误。
在左边的导航栏上,有一个部分写着"Frameworks & Extensions“。您需要选择
jQuery (edge)
或该下拉列表中的任何jQuery版本。
因为您没有包含jQuery,所以$
构造函数不存在,所以错误
TypeError: cannot read property 'css' of null.
因为您试图访问不存在的(空)对象的属性而引发。
https://stackoverflow.com/questions/27887933
复制相似问题