【发布时间】:2012-04-04 10:52:17
【问题描述】:
我在使用一个简单的复选框弹出窗口警报时遇到问题,我终其一生都无法弄清楚原因。这是基本的 Javascript 和 HTML:
var blue_box=document.getElementById("color-blue");
function colorFunction() {
window.alert("This color is blue!");
}
blue_box.onclick=colorFunction;
<form action="" method="POST" id="form">
<fieldset>
<legend> Form!</legend>
<ul>
<li><label><input type="checkbox"
name="color"
value="blue"
id="color-blue" />
blue</label>
</li>
<li><label><input type="checkbox"
name="color"
value="red"
id="color-red" />
red</label>
</li>
<li><label><input type="checkbox"
name="color"
value="yellow"
id="color-yellow" />
yellow </label>
</li>
</ul>
</fieldset>
</form>
哪个抛出:Uncaught TypeError: Cannot set property 'onclick' of null
下
blue_box.onclick=colorFunction;
我的代码中出现此错误是否有任何明显的原因?
【问题讨论】:
-
你把脚本标签放在body元素的end了吗? (在
标签: javascript events handler