<script>
window.onload=function(){
alert(document.getElementById("aaa").className);
document.getElementById("aaa").className = "red";
};
</script>
<style type="text/css">
div.green:hover{background-color:Green;}
div.red:hover{background-color:red;}
</style>
<div   id="aaa">aaa</div>
===============================
 这样应该是可以的吧!

解决方案 »

  1.   

    小#,我就想知道脚本能否动态创建这样的样式,而不是事先写好一个<style>感谢参与
      

  2.   

    function TEST(){
    if(document.all)
    {
    window.style="div:hover{background-color:Green;}";
    document.createStyleSheet("javascript:style");
    }
    else
    {
    var style = document.createElement('style');
    style.type = 'text/css';
    style.innerHTML="div:hover{ background-color:Green}";
    document.getElementsByTagName('HEAD').item(0).appendChild(style);
    }
    }
      

  3.   

    <style   type='text/css'>   
      <!--   
      a:hover           {   
      text-decoration:   underline;   
      font-family:   '新細明體';   
      font-size:   9pt;   
      color:   #FFFFFF;   
      }   
      .link   {   
      font-family:   '新細明體';   
      font-size:   9pt;   
      color:   #DBD9FF;   
      text-decoration:   none;   
      }   
      -->   
      </style>   
     <a   href="#"   target="main"   class="link">STEP</a>
    <script>   
      document.styleSheets[0].rules("a:hover").style.color="green";   
      </script>
      

  4.   

    <!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>Demo</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <style type="text/css">
        div{width:20px;height:20px;background-color:Red;}
        div:hover{background-color:Green;}
        </style>
    </head>
    <body>
        <div></div>
    <script>   
      document.styleSheets[0].rules("div:hover").style.background="green";   
      </script>
    </body>
    </html>
      

  5.   

    其实我期待的是这样的写法,看来是木有了
    element.style.hover.width = "20px";
    element.style.hover.height = "20px";
    element.style.hover.backgroundColor = "Red";第二个问题看来也是比较偏的,下班时结了。
      

  6.   

    汗!才想起<div style=""></div>这样的没有hover属性!!!!
      

  7.   

    <script>
    var doc = document;
    var div = doc.createElement("DIV");
    div.id = "";
    div.zIndex = "1";
    div.style.left = "0px";
    div.style.top = "0px";
    div.style.width = "200px";
    div.style.height = "200px";
    div.className="aa"; 
    div.style.position = "absolute";
    div.style.backgroundColor = "#EFEFEF";
    document.styleSheets[0].rules("aa:hover").style.background="green";
    div.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=50)";
    doc.body.appendChild(div);
    window.onload=function(){alert(document.styleSheets[0].rules("aa:hover").style.background)}
    </script>我用了一个变换的形式,但是好象没有效果,晕!
      

  8.   

    <script>
    var doc = document;
    var div = doc.createElement("DIV");
    div.id = "";
    div.zIndex = "1";
    div.style.left = "0px";
    div.style.top = "0px";
    div.style.width = "200px";
    div.style.height = "200px";
    div.className="aa"; 
    div.style.position = "absolute";
    document.styleSheets[0].rules("aa").style.background="#EFEFEF";
    document.styleSheets[0].rules("aa:hover").style.background="green";
    div.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=50)";
    doc.body.appendChild(div);
    window.onload=function(){alert(document.styleSheets[0].rules("aa:hover").style.background)}
    </script>这样他就有效了
      

  9.   

    动态创建class哈哈上次有一个人问的动态创建class我研究半天,当时没弄出来今天你问的时候让我突然间想起了用这么一个属性
      

  10.   

    <!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>Demo</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <style type="text/css">
        div{width:20px;height:20px;background-color:Red;}
        div:hover{}
        </style>
    </head>
    <body>
        <div style="h"></div><script>
    var doc = document;
    var div = doc.createElement("DIV");
    div.id = "";
    div.zIndex = "1";
    div.style.left = "0px";
    div.style.top = "0px";
    div.style.width = "200px";
    div.style.height = "200px";
    div.className="aa"; 
    div.style.position = "absolute";
    document.styleSheets[0].rules("aa").style.background="#EFEFEF";
    document.styleSheets[0].rules("aa:hover").style.background="green";
    div.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=50)";
    doc.body.appendChild(div);
    </script>
    </body>
    </html>
      

  11.   

    觉得用特定的class定义需要有效果的元素更好点按楼主的代码比较麻烦的搞了下 :), 考虑的不全, 比如hover还有父容器的情况这样 .abc .cba div:hover{***} 太麻烦啦<!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>Demo</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <style type="text/css" id="style">
        div{width:20px;height:20px;background-color:Red;}
        div:hover{background-color:Green;} div.cc{background-color:blue} span{display:block; width:100px; height:20px; background:red}
    span:hover{background:yellow}
        </style>
    </head>
    <body>
        <div></div>
    <div class="cc"></div>
    <span></span>
        <script>   
    var styles = document.getElementById('style').innerHTML, reg01 = /[^\}]+?\}/g, cssEs = styles.match(reg01), els = [];

    var reg02 = /[^,]+(?=,|\{)/g;
    for(var i=0; i<cssEs.length; i++){
    var ces = cssEs[i].match(reg02), css = cssEs[i].replace(/^[^\{]+?\{(.+?)\}$/,'$1');
    for(var n=0; n<ces.length; n++){
    var hs = ces[n].match(/[\w_]+(?=:hover)/g);
    if(hs)for(var m=0; m<hs.length; m++)getEv(hs[m].replace(/^\s*?(.+)\s*?$/,'$1'), css);
    }
    } function getEv(hs, css){
    if(/^a\./i.test(hs)) return;
    var tem = hs.split('.'), elm = tem[0], cls = tem[1] || null;
    var elms = document.getElementsByTagName(elm);
    if(cls){
    for(var n=0; n<elms.length; n++)if(elms.className == 'cls') setEv(elms[n], css);
    }else{
    for(var n=0; n<elms.length; n++) setEv(elms[n],css);
    }
    } function setEv(elm, css){
    elm.css = css;
    elm.onmouseover = function(){
    this.style.cssText = this.css;
    this.setAttribute('style', this.css);
    }
    elm.onmouseout = function(){
    this.style.cssText = '';
    this.setAttribute('style','');
    }
    }
      </script>
    </body>
    </html>
      

  12.   

    function show(type) {
    type.prototype.show = function() { alert(this); }
    }show(String);
    show(Number);"Zswang".show();
    (1234).show();
      

  13.   

    匿名递归函数
    alert(function(n){ return n <= 1 ? 1 : n + arguments.callee(n - 1); }(10));