javascript 未捕获的类型错误:无法设置 null 的属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18219298/
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
Uncaught TypeError: Cannot set property of null
提问by user2593100
I keep on getting "Uncaught TypeError: Cannot set property of null" whenever my code hits the js code below to open a popup when clicked on a label on aspx page. Please help.
每当我的代码点击下面的 js 代码以打开一个弹出窗口时,我都会收到“未捕获的类型错误:无法设置 null 的属性”。请帮忙。
function openWindow(which) {
//alert(which);
//alert(document.getElementById('iframeClass').src);
document.getElementById('iframeClass').src = 'ViewIovationResults.aspx?ordernumber=' + which;
//alert(document.getElementById('iframeClass').src);
if (divMap.style["display"] == "none") {
divMap.style["display"] = "";
}
}
<tr>
<td width="190"><STRONG>Order Number :</STRONG></td> <td width="200"> <a onclick="javascript:openWindow('<%# DataBinder.Eval(Container.DataItem, "Order Number") %>')"><%# DataBinder.Eval(Container.DataItem, "Order Number") %></a> </td>
</tr>
采纳答案by Sergio
You should have inside the function var divMap = document.getElementById('divMap ');
before calling it. Unless its defined already outside the function and within scope.
var divMap = document.getElementById('divMap ');
在调用它之前,您应该在函数内部。除非它已经在函数之外和范围内定义。
回答by Claudio Redi
Without more information is hard to say, but if that's the problematic code then, by the time that script is executed, there is no element with id iframeClass
or/and divMap
is null.
没有更多信息很难说,但是如果那是有问题的代码,那么在执行该脚本时,没有带有 idiframeClass
或/并且divMap
为 null 的元素。
So In short:
所以简而言之:
- Do you have an html element with id
iframeClass
? - Do you have an html element with id
divMap
? - If both elements exist on the page, are you executing that script afterthey are loaded?
- 你有一个带有 id 的 html 元素
iframeClass
吗? - 你有一个带有 id 的 html 元素
divMap
吗? - 如果页面上存在这两个元素,您是否在加载它们后执行该脚本?
回答by Karl Anderson
My guess is that you do not have an element on your page with an ID value of iframeClass
. Verify the name of this element or there is no variable named divMap
.
我的猜测是您的页面上没有 ID 值为 的元素iframeClass
。验证此元素的名称或没有名为 的变量divMap
。