javascript StyleSheet樣式操作類

早上在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>

 

 

 

發佈了35 篇原創文章 · 獲贊 2 · 訪問量 16萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章