用createElement("div")生成的div对象,使用.className="xx"设置了css样式,但在ie6下没有把css应用到这个层里,在ie7,ie8下都正常,也换过setAttribute("class",xx),setAttribute("className",xx),都没起作用在线等

解决方案 »

  1.   


    //应该这样用
    var odiv=document.createElement("div");
     var att=document.createAttribute("class");
     odiv.attributes.setNamedItem(att);
    att.nodeValue="className"
      

  2.   

    <!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>
      <title> new document </title>
      <style>
    .divstyle {
    width: 300px;
    height:300px;
    border: 1px solid red;
    }
      </style>
      <script type="text/javascript">
      <!--
    function create(){
    var odiv = document.createElement("div");
    odiv.className = "divstyle";
    document.body.appendChild(odiv);
    }

      //-->
      </script>
      
     </head> <body>
      <input type="button" value=" Create " onclick="create();" />
     </body>
    </html>
    IE6没发现什么问题
      

  3.   

    我把原来外联的css改到内联就起作用了ie6的classname竟然不支持外联的css
      

  4.   

    终于解决了是原来css文件用的默认的ANSI编码,我的js都是utf-8的编码,因为编码不同访问不到css文件真郁闷