动态绑定事件不成功
<body onload= "load() ">
function load(){
var area=document.getElementsByTagName( "area ");
if(area.length> 0)
{
for(var i=0;i <area.length;i++)
{
area[i].onmouseover=function()
{
onMouseOver(this.alt);
};
area[i].onmouseout= function()
{
alert(this.alt);
};
}
}
}
alert(this.alt);生效了
onMouseOver(this.alt);没有生效,onMouseOver是一个全局的函数放在 </html> 下面
------解决方案--------------------area.length> 0???
莫非是area.value.length> 0
------解决方案--------------------try this :
var area=document.getElementsByTagName( "textarea ");
------解决方案--------------------如果不是笔误的话改成
var area=document.getElementsByTagName( "textarea ");
或者把onMouseOver 放在load方法之前
------解决方案--------------------onMouseOver这种函数名最好改一下,不要起和属性名相同的函数名。
------解决方案-------------------- <html>
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
<title> new page </title>
<script>
function load1(){
var area=document.getElementsByName( "area ");
if(area.length> 0)
{
for(var i=0;i <area.length;i++)
{
area[i].onmouseover=function()
{
onMouseOver(this);
};
area[i].onmouseout= function()
{
document.getElementById( "div1 ").innerHTML= "鼠标移 <font color=red> 出 </font> 了 "+this.name+ ",alt为 "+this.alt;
};
}
}
}
function onMouseOver(obj){
document.getElementById( "div1 ").innerHTML= "鼠标移 <font color=red> 入 </font> 了 "+obj.name+ ",alt为 "+obj.alt;
}
</script>
</head>
<body onload= "load1() ">
<input type=text alt= "ttt1 " name= "area " value= " "> <br>
<input type=text alt= "ttt2 " name= "area " value= " "> <br>
<input type=text alt= "ttt3 " name= "area " value= " "> <br>
<div id=div1> </div>
</body>
</html>