好像有效果呀,
没有别的事件时没必要用attachEvent来添加事件,这样不能支持FF了

解决方案 »

  1.   

    event.srcElement 搞不懂为什么这样用
    我只知道用this 是没问题的。
      

  2.   

    <head>
    <style type="text/css">
           .btn_style {         cursor:hand; width:84px; height:28px; background-color:red; pading:0px; border:0px; 

    </style>
    </head>
    <ul id="tt">
      <li >
          guigblk  </li>
      
      <li fdf="fdf2">
           fdfef
      </li></ul><script>
       /** 
         *   动态添加鼠标改变背景色事件 09/04/19
         *
         */ 
        function tarch(){
             //event.srcElement.style.background = "red";
             event.srcElement.className = "btn_style";
     event.srcElement.style.background = "red";
        }
        function tarch2(){
             event.srcElement.style.background = "";
        }   var idd = document.getElementById("tt");  /* 获得当前子结点的长度 */
      //var len = idd.childNodes.length;
      
      /* 循环添加鼠标事件 */
      for(var i = 0;i < idd.getElementsByTagName('li').length; i++){
        idd.getElementsByTagName('li')[i].attachEvent("onmouseover",tarch)
    idd.getElementsByTagName('li')[i].attachEvent("onmouseout",tarch2)
      } </script>表要用childNodes ,空格也会当成节点,切ie  ff解释也是不一样的。
      

  3.   

    function tarch2(){
             //event.srcElement.style.background = "";
             event.srcElement.className = "";        //隐藏上面记录,跟tarch方法统一。    }
      

  4.   

    楼主的问题,按照我上面的方法就能解决了。。
    至于:
    idd.getElementsByTagName('li')[i].attachEvent("onmouseout",tarch2) 
    idd.getElementsByTagName('li')[i].attachEvent("onmouseout",tarch2)

    如果你现在Internet Explorer浏览器下,调试时没有问题的。
    但到浏览器firefox(简称FF)下,运行的话,就不兼容了。
      

  5.   


    <head>
    <style type="text/css">
           .btn_style { 
            cursor:hand; width:84px; height:28px; background-color:red; pading:0px; border:0px; 

    </style>
    </head>
    <ul id="tt">
      <li>
          guigblk
      </li><li fdf="fdf2"><!--请注意<li>头接</li>尾-->
           fdfef
      </li>
    </ul><script>
       /** 
         *   动态添加鼠标改变背景色事件 09/04/19
         *
         */ 
        function tarch(){
             //event.srcElement.style.background = "red";
             event.srcElement.className = "btn_style";
        }
        function tarch2(){
             //event.srcElement.style.background = "";//请注意和tarch()对应
             event.srcElement.className = "";
        }   var idd = document.getElementById("tt");  /* 获得当前子结点的长度 */
      var len = idd.childNodes.length;
      
      /* 循环添加鼠标事件 */
      for(var i = 0;i < len; i++){
            idd.childNodes[i].attachEvent("onmouseover",tarch);
        idd.childNodes[i].attachEvent("onmouseout",tarch2);
      } </script>or:<head>
    <style type="text/css">
           .btn_style { 
            cursor:hand; width:84px; height:28px; background-color:red; pading:0px; border:0px; 

    </style>
    </head>
    <ul id="tt">
      <li>
          guigblk
      </li><li fdf="fdf2"><!--请注意<li>头接</li>尾-->
           fdfef
      </li>
    </ul><script>
       /** 
         *   动态添加鼠标改变背景色事件 09/04/19
         *
         */ 
        function tarch(){
             event.srcElement.style.background = "red";
             //event.srcElement.className = "btn_style";
        }
        function tarch2(){
             event.srcElement.style.background = "";//请注意和tarch()对应
             //event.srcElement.className = "";
        }   var idd = document.getElementById("tt");  /* 获得当前子结点的长度 */
      var len = idd.childNodes.length;
      
      /* 循环添加鼠标事件 */
      for(var i = 0;i < len; i++){
            idd.childNodes[i].attachEvent("onmouseover",tarch);
        idd.childNodes[i].attachEvent("onmouseout",tarch2);
      } </script>
      

  6.   

    如果事件列表中没有别的事件时可以直接使用obj.onmouseover=tarch;这样来添加事件
    否则,FF中可以obj.addEventListener("mouseover",tarch,false);
      

  7.   


    <!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>Untitled Document</title>
    <style type="text/css">
    .btn_style {
    cursor:pointer;/*鼠标指针变手形用pointer,不要用hand,hand不是w3c的标准写法,只在IE下有用*/
    width:84px;
    height:28px;
    background-color:red;
    pading:0px;
    border:0px;
    }
    .btn_style2 {
    cursor:pointer;
    width:84px;
    height:28px;
    background-color:;
    pading:0px;
    border:0px;
    }
    </style>
    </head>
    <body >
    <ul id="tt">
      <li > guigblk </li>
      <li fdf="fdf2"> fdfef </li>
    </ul>
    <script>
       /** 
         *   动态添加鼠标改变背景色事件 09/04/19
         *
         */ 
        function tarch(e){
    var evn = e?e:event;//取得事件对象,兼容FF
    var el = evn.srcElement?evn.srcElement:evn.target;//取得事件的Element,srcElement为IE的,target为FF的
            el.className = "btn_style";
        }
        function tarch2(e){
    var evn = e?e:event;
    var el = evn.srcElement?evn.srcElement:evn.target;
            el.className = "btn_style2";
        }   var idd = document.getElementById("tt").getElementsByTagName("li");//取得id为tt的ul下的所有的li  /* 获得当前子结点的长度 */
      var len = idd.length;
      
      /* 循环添加鼠标事件 */
      for(var i = 0;i < len; i++){
    if(document.attachEvent){//IE的添加事件
    idd[i].attachEvent("onmouseover",tarch);
    idd[i].attachEvent("onmouseout",tarch2);
    }else{//FF的添加事件
    idd[i].addEventListener("mouseover",tarch,false);
    idd[i].addEventListener("mouseout",tarch2,false);
    }
      } </script>
    </body>
    </html>
      

  8.   


    同意,attachEvent兼容性不好