这应该很简单...但它不是...>.<
var x = document.getElementById('x');
function snooze()
{
x.style.height = '10px';
}
执行后,我得到的错误是:
TypeError:表达式 'x' [null] 的结果不是对象。
编辑:注意,当我将 var 声明放入函数中时它会起作用。我不明白...:-(
function snooze()
{
var x = document.getElementById('x');
x.style.height = '10px';
}
最佳答案
或者:
- 页面上没有
id="x"
的元素。 - 代码在加载文档之前运行。
如果存在 id="x"
的元素,尝试:
window.onload = function () {
// your code in here
};
关于Javascript 类型错误 : result of expression is not an object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5754224/