<!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=gb2312" />
</head>
<script>var ha = document.getElementById("haha"); if(window.addEventListener)
{
ha.addEventListener("click",function(){alert("haha");});
}else{
ha.attchEvent("onclick",function(){alert("haha");})
}
</script>
<body>
<input type="button" id="haha" value="haha" />
</body>
</html>
我是完全的新手- -!!
我在网看了JS添加事件,试做一下它
如上面的:
完全没有反应。
请各位帮一下。
看还要哪些代码,还是错了哪些代码。谢谢
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
</head>
<script>var ha = document.getElementById("haha"); if(window.addEventListener)
{
ha.addEventListener("click",function(){alert("haha");});
}else{
ha.attchEvent("onclick",function(){alert("haha");})
}
</script>
<body>
<input type="button" id="haha" value="haha" />
</body>
</html>
我是完全的新手- -!!
我在网看了JS添加事件,试做一下它
如上面的:
完全没有反应。
请各位帮一下。
看还要哪些代码,还是错了哪些代码。谢谢
<!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=gb2312" />
</head> <body>
<input type="button" id="haha" value="haha" />
<script>
var ha = document.getElementById("haha");
if(window.addEventListener)
{
ha.addEventListener("click",function(){alert("haha");});
}else{
ha.attachEvent("onclick",function(){alert("haha");})
}
</script>
</body>
</html>
<!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=gb2312" />
</head>
<script>
window.onload=function(){
var ha = document.getElementById("haha"); if(window.addEventListener)
{
ha.addEventListener("click",function(){alert("haha");});
}else{
ha.attachEvent("onclick",function(){alert("haha");})
}
}
</script>
<body>
<input type="button" id="haha" value="haha" />
</body>
</html>
1、js的位置不对。
把<script></script>中的代码放到button之后。给对象注册事件,首先对象必须已经加载进来了。2、ha.attchEvent("onclick",function(){alert("haha");})
attchEvent拼错了,改为attachEvent
window.onload=function()
{}
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
</head><body>
<input type="button" name ="haha" id="haha" value="haha" />
</body>
</html>
<SCRIPT LANGUAGE="JavaScript">
<!--
var ha = window.document.getElementById("haha");
//alert(ha);null
//if(typeof (ha) =="object")alert("ffff");
if(window.addEventListener)
{
ha.addEventListener("click",function(){alert("haha");},false);
}else{
ha.attachEvent("onclick",function(){alert("haha");})
}
-->
</SCRIPT>
错误三点:
1:顺序,可以放到onload里或者放到后面,加载时
2:在ie内核里是用
//object.attachEvent(event,function);
在fifefox里用
//element.addEventListener(type,listener,useCapture);