var btn2 = document.getElementById("btn2");btn2.onclick = function(){alert("这是通过DOMO级添加的事件!");}——这一段要是放在head标签里,运行无反应,错误提示:Uncaught TypeError: Cannot set property 'onClick' of null。要把代码放在body标签才可以运行。
木言纱
2014-08-01
1 回答
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script>
window.onload = function(){
var oBtn = document.getElementById('btn2');
oBtn.onclick = function(){
alert('DOM0级事件处理程序.');
};
};
</script>
</head>
<body>
<input type="button" id="btn2" value="Click"/>
</body>
</html>我的这段代码运行没有问题。你可以把代码贴出来。
举报
0/150
提交
取消