var myArray=document.getElementsByTagName("A") ;
//查找网页中所有的A对像
var Length = myArray.length;
//length为对像的个数
for(var i = 0; i < Length; i++)
{
obj=myArray[i];
//obj为当前所有找到的<A>中的某一个
obj.onmouseover="alert('')";
         //给这个<a>添加onmouseover事件         

出错就在这一句:
obj.onmouseover="alert('')";
本来感觉是没有错的,但是网页中就是不会执行onmouseover事件大家看看哪里出错

解决方案 »

  1.   

    <script defer="defer">
    var myArray=document.getElementsByTagName("A") ;
    var Length = myArray.length;
    for(var i = 0; i < Length; i++)
    {
    obj=myArray[i];
    obj.onmouseover=function(){this.href = 'http://www.163.com';}
    }
    </script>
      

  2.   

    obj.onmouseover="this.href='http://www.163.com'";
    这样只是把一个字符串赋给onmouseover
      

  3.   


    <TABLE id=menu height="246" cellSpacing="0" cellPadding="0" width="43%" align="right" border="0">
    <TR>
    <TD height="32"><A href="brief.aspx"  onmouseover="this.firstChild.src='images/0.gif'"
    onmouseout="this.firstChild.src='images/100.gif'"><IMG height="54" src="images/100.gif" width="104" border="0"></A></TD>
    </TR>
    <TR>
    <TD height="2">
    <DIV align="center"><IMG height="8" src="images/link.gif" width="100"></DIV>
    </TD>
    </TR>
    <TR>
    <TD height="8"><A href=product.aspx  onmouseover="this.firstChild.src='images/1.gif'"
    onmouseout="this.firstChild.src='images/101.gif'" ><IMG height="54" src="images/101.gif" width="104" border="0"></A></TD>
    </TR>
    <TR>
    <TD height="6">
    <DIV align="center"><IMG height="8" src="images/link.gif" width="100"></DIV>
    </TD>
    </TR>
    <TR>
    <TD><A href=trade.aspx   onmouseover="this.firstChild.src='images/2.gif'"
    onmouseout="this.firstChild.src='images/102.gif'"><IMG height="54" src="images/102.gif" width="104" border="0" ></A></TD>
    </TR>
    <TR>
    <TD>
    <DIV align="center"><IMG height="8" src="images/link.gif" width="100"></DIV>
    </TD>
    </TR>
    <TR>
    <TD><A href='content'  onmouseover="this.firstChild.src='images/3.gif'"
    onmouseout="this.firstChild.src='images/103.gif'"><IMG height="60" src="images/103.gif" width="104" border="0"></A></TD>
    </TR>
    <TR>
    <TD><BR>
    <BR>
    <A href='' 
    ><IMG height="60" src="images/104.gif" width="104" border="0""></A></TD>
    </TR>
    </TABLE>
    <SCRIPT language=JavaScript type=text/JavaScript>
    var url=Geturl(location.href);
    var obj;
    var myArray=document.getElementById("menu").document.getElementsByTagName("A") ;
    var Length = myArray.length;
    for(var i = 0; i < Length; i++)
    {
    obj=myArray[i];
    obj.onmouseover=function() {"alert('');"};                               //"this.firstChild.src='images/"+i+".gif'"};
    obj.onmouseout=function() {"alert('')"}                                           //{"this.firstChild.src='images/"+(100+i)+".gif'"};
    if(Geturl(obj.href)==url)
    {
    obj.parentElement.innerHTML="<img height='60' src='images/"+i+".gif' width='104' border='0'/>"
    break;
    }
    }function Geturl(url)
    {
    var lastindex=url.lastIndexOf('/');
    url=url.substring(lastindex+1,url.length);
    var fristindex=url.indexOf('?');
    if(fristindex!=-1)
    {
    url=url.substring(0,fristindex)
    }
    return url;
    }</script>不行上面不会执行alert('')
      

  4.   

    OK了
    把obj.onmouseover=function() {"alert('');"};  
    改成obj.onmouseover=function() {alert('');};
      

  5.   

    经典的闭包问题
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <TITLE> New Document </TITLE>
    <META NAME="Generator" CONTENT="EditPlus">
    <META NAME="Author" CONTENT="">
    <META NAME="Keywords" CONTENT="">
    <META NAME="Description" CONTENT="">
    </HEAD><BODY>
    <script defer="defer">
    var myArray=document.getElementsByTagName("A") ;
    var Length = myArray.length;
    for(var i = 0; i < Length; i++)
    {
    obj=myArray[i];
    obj.onmouseover=function(){this.href = 'http://www.163.com';}
    }
    </script>
    <a href="test2">test1</a>
    <a href="test2">test2</a>
    </BODY>
    </HTML>