js列舉css中所有圖標的實現(xiàn)代碼
更新時間:2011年07月04日 23:31:24 作者:
最近在做一個通用的配置模塊。配置模塊中必然要對系統(tǒng)的菜單、功能模塊、權(quán)限資源等進行配置,為了更好的用戶體驗,圖標是必不可少的!
美工做的css樣式里icon越來越多,手工來寫icon名幾乎不可能實現(xiàn)。所以就有了這個功能點:將css樣式里的所有icon類列舉出來以圖形化的形式顯示供配置人員選擇!
搜索一圈,發(fā)現(xiàn)介紹從css里遍歷東西的文章非常少。于是花了半天的時間自己來實現(xiàn)了!先看下一個選擇圖標的demo:
<input id="cc" class="easyui-combotree" style="width:200px;">
Javascript:
function getstyle() {
for (var i = 0; i < document.styleSheets.length; i++) {
var rules;
if (document.styleSheets[i].cssRules) {
rules = document.styleSheets[i].cssRules;
}
else {
rules = document.styleSheets[i].rules;
}
for (var j = 0; j < rules.length; j++) {
if (rules[j].selectorText.substr(0, 5) == ".icon")
$('#cc').combotree('tree').tree('append', {
data: [{
id: rules[j].selectorText.substr(1),
text: rules[j].selectorText.substr(1),
iconCls: rules[j].selectorText.substr(1)
}]
});
}
}
}
$(function () {
getstyle();
});
有幾個可能存在的問題:
1.大的項目樣式可能巨多巨大,這種遍歷顯然不可能,需要指定到styleSheets。
2.如果各種大小的圖標可能用easyui-combotree不太合理。
最后通過配置icon生成的菜單效果:
搜索一圈,發(fā)現(xiàn)介紹從css里遍歷東西的文章非常少。于是花了半天的時間自己來實現(xiàn)了!先看下一個選擇圖標的demo:
這里用的是easyui:一個comboxtree。
這里省略引用css和js庫的代碼(jquery和easyui庫)。
html:
復制代碼 代碼如下:
<input id="cc" class="easyui-combotree" style="width:200px;">
Javascript:
復制代碼 代碼如下:
function getstyle() {
for (var i = 0; i < document.styleSheets.length; i++) {
var rules;
if (document.styleSheets[i].cssRules) {
rules = document.styleSheets[i].cssRules;
}
else {
rules = document.styleSheets[i].rules;
}
for (var j = 0; j < rules.length; j++) {
if (rules[j].selectorText.substr(0, 5) == ".icon")
$('#cc').combotree('tree').tree('append', {
data: [{
id: rules[j].selectorText.substr(1),
text: rules[j].selectorText.substr(1),
iconCls: rules[j].selectorText.substr(1)
}]
});
}
}
}
$(function () {
getstyle();
});
有幾個可能存在的問題:
1.大的項目樣式可能巨多巨大,這種遍歷顯然不可能,需要指定到styleSheets。
2.如果各種大小的圖標可能用easyui-combotree不太合理。
最后通過配置icon生成的菜單效果:
