用户操作
[即时聊天] [发私信] [加为好友]
卞元锋ID:CutBug
4699次访问,排名19304(2),好友0人,关注者0人。
所谓好的程序员就是他写的东西有人用,而且用起来方便!
CutBug的文章
原创 13 篇
翻译 0 篇
转载 4 篇
评论 24 篇
CutBug的公告
最近评论
文章分类
收藏
    相册
    B/S开发
    Code Project
    CoolCode
    meizz Blog
    new-online
    prototype框架官方网站
    思归blog
    C/S开发
    数据库-Oracle
    数据库-SQLServer
    存档
    软件项目交易
    订阅我的博客
    XML聚合  FeedSky
    订阅到鲜果
    订阅到Google
    订阅到抓虾
    订阅到BlogLines
    订阅到Yahoo
    订阅到GouGou
    订阅到飞鸽
    订阅到Rojo
    订阅到newsgator
    订阅到netvibes

    原创 javascript StyleSheet样式操作类收藏

    新一篇: javascript 用克隆的方法动态添加表格行 | 旧一篇: javascript动态改变style里的样式

    早上在csdn上看有人问页面style sheet怎么修改里面的rule,就写了个类,该类对兼容FF和IE做了处理。

    /*--------------------------------------------
        描述 : 添加新的样式rule
        参数 : styleSheets索引
        代码 : var ss = new styleSheet(0);
    --------------------------------------------
    */

    var styleSheet = function(n)
    {
        
    var ss;
        
    if (typeof n == "number") ss = document.styleSheets[n];
        
    this.sheet = ss;
        
    this.rules = ss.cssRules?ss.cssRules:ss.rules;
    }
    ;

    /*--------------------------------------------
        描述 : 查找样式rule,成功返回index,否则返回-1
        参数 : n为rule名称
        代码 : var ss = new styleSheet(0);
              ss.indexOf("className")
    --------------------------------------------
    */

    styleSheet.prototype.indexOf 
    = function(selector)
    {
        
    for(var i=0;i<this.rules.length;i++)
        
    {
            
    if(this.rules[i].selectorText==selector)
            
    {
                
    return i;
            }

        }

        
    return -1;
    }
    ;



    /*--------------------------------------------
        描述 : 删除样式rule
        参数 : n为rule索引或者名称
        代码 : var ss = new styleSheet(0);
              ss.removeRule(0) || ss.removeRule("className")
    --------------------------------------------
    */

    styleSheet.prototype.removeRule 
    = function(n)
    {
        
    if(typeof n == "number")
        
    {
            
    if(n<this.rules.length)
            
    {
                
    this.sheet.removeRule?this.sheet.removeRule(n):this.sheet.deleteRule(n);
            }

        }
    else
        
    {
            
    var i = this.indexOf(n);
            
    this.sheet.removeRule?this.sheet.removeRule(i):this.sheet.deleteRule(i);
           
        }

    }
    ;

    /*--------------------------------------------
        描述 : 添加新的样式rule
        参数 : selector      样式rule名称
              styles        样式rule的style
              n             位置
        代码 : var ss = new styleSheet(0);
              ss.addRule("className","color:red",0);
    --------------------------------------------
    */

    styleSheet.prototype.addRule 
    = function(selector,styles,n)
    {
        
    if(typeof n == "undefined")
        
    {
            n 
    = this.rules.length;
        }

        
    this.sheet.insertRule?this.sheet.insertRule(selector + "{" + styles + "}", n):this.sheet.addRule(selector, styles, n);
       
    }
    ;

    /*--------------------------------------------
        描述 : 设置样式rule具体的属性
        参数 : selector      样式rule名称
              attribute     样式rule的属性
              _value        指定value值
        代码 : var ss = new styleSheet(0);
               ss.setRuleStyle("className","color:","green");
    --------------------------------------------
    */

    styleSheet.prototype.setRuleStyle 
    = function(selector,attribute,_value)
    {
        
    var i = this.indexOf(selector);
        
    this.rules[i].style[attribute] = _value;
    }
    ;

    /*--------------------------------------------
        描述 : 获得样式rule具体的属性
        参数 : selector      样式rule名称
              attribute      样式rule的属性
        代码 : var ss = new styleSheet(0);
              ss.getRuleStyle("className","color");
    --------------------------------------------
    */

    styleSheet.prototype.getRuleStyle 
    = function(selector,attribute)
    {
        
    var i = this.indexOf(selector);
        
    return this.rules[i].style[attribute];
    }
    ;

    使用的例子,使用setRuleStyle方法将#pid的color改成green

    <style type="text/css" > 
    #pid 
    {color: red;} 
    </style> 
    <id="pid">22 </> 
    <input type="button" onclick="test()" value=" test " />
    <script language="javascript" type="text/javascript" > 
    function test()
    {
        
    var sheet = new styleSheet(0);
        sheet.setRuleStyle(
    "#pid","color","green");
    }

    </script>

     

     

     

    发表于 @ 2007年10月24日 14:19:00|评论(loading...)|编辑

    新一篇: javascript 用克隆的方法动态添加表格行 | 旧一篇: javascript动态改变style里的样式

    评论:没有评论。

    发表评论  


    登录
    Csdn Blog version 3.1a
    Copyright © CutBug