日期:2014-05-16  浏览次数:20449 次

用Javascript调用css的类
怎样用Javascript代码调用css中的类,请高手指点

------解决方案--------------------
如:<div id="div1"></div>
css:
.divCss
js:
document.getElementsByID("div1").className="divCss";
------解决方案--------------------
dom中class就是 楼主所说
style中就这样
HTML code

<!DOCTYPE HTML>
<html>
    <head>
        <meta charset="gb2312" />
        <title></title>        
    </head>
    <body>
        <div class="a">111111111</div>
        <div class="b">111111111</div>
        <div class="c">111111111</div>
        <script>
            function $(el){
                return typeof el == 'string' ? document.getElementById(el) : el;
            }
            
            ;(function(w, d){
                var ie = !+[1,];
                var o = null;
                var styleSheet = function(){
                    this.init();
                };
                styleSheet.prototype = {
                    init: function(){
                        var h = d.getElementsByTagName('head')[0];
                        var s = d.createElement('style');
                        h.appendChild(s);
                        o = d.styleSheets[d.styleSheets.length-1];
                    },
                    add: function(selector, style){
                        ie ?
                            o.addRule(selector, style)
                            :
                            o.insertRule(selector + "{" + style + "}", o.cssRules.length);
                    },
                    del: function(index){
                        ie ?
                            o.removeRule(index)
                            :
                            o.deleteRule(index);
                    },
                    getRules: function(){
                        return ie ?  
                            o.rules
                            :
                            o.cssRules;                    
                    },
                    getRule: function(selector){
                        var rules = this.getRules();
                        for(var i = 0, len = rules.length; i < len; i++){
                            var r = rules[i];
                            if( r.selectorText == selector ){
                                return r;
                            }
                        }
                        return null;
                    }
                }
                w.styleSheet = styleSheet;
            })(window, document);
            
            var s = new styleSheet;
            s.add('.a', 'color:red;')
            s.add('.a', 'font-size:111px;')
            s.add('.a', 'font-size:11px;')
            s.add('.b', 'color:blue;')
            //s.del(0)
            alert( s.getRule('.a').style['color'] )
                        
        </script>
    </body>
</html>