twitter-bootstrap 类型错误:无法读取 null 的属性“css”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27887933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: cannot read property 'css' of null
提问by Neophile
I am trying to set the minimum-height of my div element by referring to its class name but it comes back with an error: TypeError: cannot read property 'css' of null.
我试图通过引用它的类名来设置我的 div 元素的最小高度,但它返回一个错误:TypeError: cannot read property 'css' of null。
HTML element:
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:
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>
Any suggestions as to why am I getting this error? Kindly let me know.
关于为什么我收到此错误的任何建议?请让我知道。
The problem occurs when I include my external javascript file:
当我包含我的外部 javascript 文件时会出现问题:
<!-- <script type="text/javascript" src="js/monthly.js"></script> -->
回答by Ja?ck
You're loading jQuery and Bootstrap at the same time and they both clobber the $symbol; to use only jQuery you would use the canonical name:
您同时加载 jQuery 和 Bootstrap,它们都破坏了$符号;要仅使用 jQuery,您将使用规范名称:
jQuery(".contentContainer")
.css('min-height', jQuery(window).height());
Or, use a wrapper:
或者,使用包装器:
jQuery(function($) {
$(".contentContainer").css('min-height',$(window).height());
});
回答by Thomas Bormans
This is the jQuery syntax.
这是 jQuery 语法。
$(window).height();
You will need to include jQuery or use the native JavaScript version:
您将需要包含 jQuery 或使用原生 JavaScript 版本:
window.innerHeight;
In order to use jQuery, you will need to include the jQuery library
为了使用 jQuery,您需要包含jQuery 库
回答by theonlygusti
You were getting the error on the JSFiddle because you hadn't included jQuery.
您在 JSFiddle 上收到错误,因为您没有包含 jQuery。
On the left nav-bar, there is a section which says "Frameworks & Extensions". You need to select
在左侧导航栏上,有一个部分写着“框架和扩展”。你需要选择
jQuery (edge)
Or any jQuery version from that drop-down list.
或该下拉列表中的任何 jQuery 版本。


Because you hadn't included jQuery, the $constructor didn't exist, so the error
因为你没有包含 jQuery,$构造函数不存在,所以错误
TypeError: cannot read property 'css' of null.
was thrown because you were trying to access a property of a non-existent (null) object.
被抛出是因为您试图访问不存在(空)对象的属性。
回答by bluefog
var x = document.getElementsByClassName("contentContainer");
Returns an array of elements having that class. Access the required element as you would in a normal array.
返回具有该类的元素数组。像在普通数组中一样访问所需的元素。
x[0] //The first element, if there is any.
回答by filipemgs
If you type $ [ENTER] on your browser console do you get something like
如果你在你的浏览器控制台上输入 $ [ENTER] 你会得到类似的东西
function (a,b){return new e.fn.init(a,b,h)
If the selector $('.contentContainer') returns null and not an object containing function css, maybe $ is not referencing to jQuery or has been changed beetween where you include jquery.js and the code you are trying to run.
如果选择器 $('.contentContainer') 返回 null 而不是包含函数 css 的对象,则 $ 可能没有引用 jQuery 或者在包含 jquery.js 的位置和您尝试运行的代码之间发生了更改。
回答by markw707
If your error is occurring in the use of bootstrap-dialog, comment out the following line of code in the bootstrap-dialog.js file:
如果你的错误是在使用 bootstrap-dialog 时出现的,请在 bootstrap-dialog.js 文件中注释掉以下代码行:
$backdrop.css('z-index', zIndexBackdrop + (dialogCount - 1) * 20);
$backdrop.css('z-index', zIndexBackdrop + (dialogCount - 1) * 20);
This stops the "TypeError: cannot read property 'css' of null" error.
这将停止“TypeError: cannot read property 'css' of null”错误。
If your error is not occurring in the use of bootstrap-dialog, open the relevant js file, then comment out all of the css file references in it and re-run the web page. It should run successfully. Then uncomment the lines of code one by one and re-test the page until it breaks again. This will eventually lead you to the villain line of code, then you can comment it out and avoid this error going forward.
如果在使用bootstrap-dialog时没有出现你的错误,打开相关的js文件,然后把里面所有的css文件引用注释掉,重新运行网页。它应该成功运行。然后一一取消注释代码行并重新测试页面,直到它再次中断。这最终将引导您到反派代码行,然后您可以将其注释掉并避免此错误继续进行。

