JavaScript��DOM���Ǥ�style°���򤤤���Τϴ�ñ�Ǥ��������줬CSS�Ȥ�ʤ��object tree�����������Ѥǡ�Web�򸫲󤷤Ƥ�ۤȤ�ɻ����㤬����ޤ���ưŪ��CSS�򤤤��äƤ��륵���Ȥ�������ݤ�����̣��Web�ǥ��������餤�����������ޤ���
�Ȥ����櫓�ǡ�������ͤơ�style°���ǤϤʤ�CSS��ޤ뤴�Ƚñ¤´ï¿½ï¿½ï¿½ï¿½ï¿½Ø¿ï¿½ï¿½ï¿½ï¿½Ã¤Æ¤ß¤Þ¤ï¿½ï¿½ï¿½ï¿½ï¿½
- CSS Manipulation via JavaScript
- http://www.dan.co.jp/~dankogai/css/css_handlers.html
����
ư���ǧ��Firefox 2, Opera 9, Safari, Mac IE5�ǹԤäƤ��ޤ���Safari�ǤΤߤʤ���CSS���ѹ����Ǥ��ޤ��󤬡�CSS���ɤळ�ȤϽ���ޤ���
styleSheets2Obj()
��CSS��������ä�object���֤��ޤ���Selector��������CSSText���͡��ɤ����ʸ����ʤΤ�ľ��DOM Stylesheet��á������ưפ˰����ޤ���
Mac IE5�ǤϤʤ���Selector��Ƭ��'*'���Ĥ��Ƥ��ޤ�����¾��IE�ǤϤɤ��Ǥ��礦?
getCSSTextBySelector(selector)
selector�ǻ��ꤵ�줿CSS������֤��ޤ���
setCSSTextBySelector(selector, text)
selector�ǻ��ꤵ�줿CSS�����text�ǽñ¤´ï¿½ï¿½ï¿½ï¿½Þ¤ï¿½ï¿½ï¿½selector���Ժߤʾ��Ͽ������ɲä��ޤ���
safari�ǤϤʤ���ư���ޤ���
addCSSTextBySelector(selector, text)
selector�ǻ��ꤵ�줿CSS����ˡ�text��ä��ޤ���selector���Ժߤʾ��Ͽ������ɲä��ޤ���
�������safari�ǤϤʤ���ư���ޤ���
Enjoy!
Dan the Man with Style
Source Codefunction styleSheets2Obj(){ var result = {}; var stylesheets = document.styleSheets; for (var s = 0, sl = stylesheets.length; s < sl; s++){ if (stylesheets[s].disabled) continue; var rules = stylesheets[s].rules || stylesheets[s].cssRules; for (var r = 0, rl = rules.length; r < rl; r++){ result[rules[r].selectorText] = rules[r].style.cssText; } } return result; } function getCSSTextBySelector(selector){ return styleSheets2Obj()[selector] } function setCSSTextBySelector(selector, text){ var stylesheets = document.styleSheets; for (var s = 0, sl = stylesheets.length; s < sl; s++){ if (stylesheets[s].disabled) continue; ; for (var r = 0, rl = rules.length; r < rl; r++){ // 4 IE var selectorText = rules[r].selectorText.toLowerCase(); var sel = selector.toLowerCase(); if (selectorText.charAt(0) == '*') sel = '*' + sel; if (selectorText == sel){ stylesheets[s].deleteRule(r); stylesheets[s].insertRule(selector + '{' + text + '}', r); return 0; } } } var lastss = stylesheets[stylesheets.length - 1]; lastss.insertRule(selector + '{' + text + '}', lastss.length); return 1; } function addCSSTextBySelector(selector, text){ var stylesheets = document.styleSheets; for (var s = 0, sl = stylesheets.length; s < sl; s++){ if (stylesheets[s].disabled) continue; var rules = stylesheets[s].rules || stylesheets[s].cssRules; for (var r = 0, rl = rules.length; r < rl; r++){ // 4 IE var selectorText = rules[r].selectorText.toLowerCase(); var sel = selector.toLowerCase(); if (selectorText.charAt(0) == '*') sel = '*' + sel; if (selectorText == sel){ var t = rules[r].style.cssText; stylesheets[s].deleteRule(r); stylesheets[s].insertRule(selector + '{' + t + text + '}', r); return 0; } } } var lastss = stylesheets[stylesheets.length - 1]; lastss.insertRule(selector + '{' + text + '}', lastss.length); return 1; }
�ɵ�:
�ؤäؤä�: ��̱�ɼˤϤ�ڤ� �������䤳�������Ȥˡ�Windows �� IE �Τߡ�cssRules �ǤϤʤ���rules �ȤʤäƤ��ޤ���
��򡢤���𤢤꤬�Ȥ��������ޤ���ȿ�Ǥ����ޤ�����
���Υ֥����˥����Ȥ���ˤ�����������ɬ�פǤ���
��������������
���ε����ˤϵ��ĥ桼�����������Ȥ��Ǥ��ޤ���