【发布时间】:2011-05-08 21:28:13
【问题描述】:
我有这个 html 代码
<div id = 'status'>
<span class="formw" style="background-color:#FBD9E5;">
some text
</span>
</div>
当下面的 javascript 被执行时
$('status').style.backgroundColor = '#99e83f';
chrome 抛出以下错误:
Uncaught TypeError: Cannot read property 'style' of null
有人知道原因吗?
更新:我正在使用原型。
【问题讨论】:
-
@user ,我们是否有名为 backgroundcolor fordiv 的属性,它应该是颜色正确或背景图像 url
-
我相信你错过了
ID的#,请执行:$('#status'),我假设你在这里使用 jQuery。 -
@gov - 是的,该属性是有效的。 @user - 这可以与 document.getElementById() 一起使用吗?
-
@steve,你说得对,我忘了。
-
如果是jQuery,那么
.style.foo也是错误的(它会抱怨style为空,而不是$的返回值是。(mutter,mutter,@ 987654331@ 是一个可恶的变量名:blog.dorward.me.uk/2009/02/19/the-dollar-function-must-die.html)。我猜测$直接映射到document.getElementById并且 JS 在 div 出现在源代码之前运行代码,但问题缺少太多信息,只是盲目地四处寻找。
标签: javascript css google-chrome prototypejs