getComputedStyle ã¨ã¯ï¼ï¼
ããè¦ç´ ã«ã©ããªã¹ã¿ã¤ã«ãå½ãã£ã¦ããããè¨ç®ãã¦ãããã便å©ãªé¢æ°ã
使ãããã¯ãã£ã¡ãç°¡åï¼
var style = getComputedStyle(element, ''); alert(style.fontSize); // 14px alert(style.color); // rgb(0, 0, 0)
ã¡ãªã¿ã«ç¬¬äºå¼æ°ã¯çä¼¼è¦ç´ ã® style ãåãããå ´åã«ä½¿ãã¾ããé常ã¯ç©ºæååã§ããã
ã§ãã getComputedStyle ã¯ãã®ã¾ã¾ã§ã¯ IE, Safari ã§ã¯åããªãã
Safari ã§ã¯
window(ã°ãã¼ãã«é å) ã« getComputedStyle ã¯å®ç¾©ããã¦ãªãã¦ã document.defaultView ã ãã« getComputedStyle ãå®ç¾©ããã¦ããã
ã¡ãªã¿ã«ã Firefox, Opera ã§ã¯ document.defaultView == window ãªã®ã§ã以ä¸ã®ããã«ãããã¨ã§ Firefox, Opera, Safari ã§ä½¿ããã¨ãã§ããã
var style = document.defaultView.getComputedStyle(element, '')
IE ã§ã¯
ãããã getComputedStyle ã¨ããé¢æ°èªä½ãåå¨ããªã
ãã®ä»£ããã IE ã§ã¯ãã¹ã¦ã® HTMLElement ã currentStyle ã¨ããããããã£ãæã£ã¦ãã¦ãããã getComputedStyle ã§åå¾ã§ãããªãã¸ã§ã¯ãã¨ã»ã¼åãå½¹å²ãããã
ã¨ãããã¨ã§ããã£ãã®ä¾ã¯ä»¥ä¸ã®ããã«ãã㨠IE ã«ã対å¿ã§ããã
var style = element.currentStyle || document.defaultView.getComputedStyle(element, '')
ã¨ãã訳ã§âããã使ãã°ãã¯ãã¹ãã©ã¦ã¶ã§ããããã¯ã³ã©ã¤ãã¼ã§ getComputedStyle ã使ããï¼ï¼ãã©ä¾¿å©ï¼ï¼
ã¨ãããã§ãçµãã£ã¦ããã°è¯ãã£ãã®ã§ãããããæ°ã«ãªã£ã¦ãã
getComytedStyle ã«ã¤ãã¦è©³ãã調ã¹ã¦ã¿ãã
getComputedStyle ã¯ä»¥ä¸ã®ããã« ViewCSS ã¨ããã¤ã³ã¿ãã§ã¼ã¹ã«å®ç¾©ããã¦ããã
// Introduced in DOM Level 2: interface ViewCSS : views::AbstractView { CSSStyleDeclaration getComputedStyle(in Element elt, in DOMString pseudoElt); };http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-OverrideAndComputed
ããã¦ã Window Object èæ¡ã§ã¯ Window ãªãã¸ã§ã¯ã㯠ViewCSS ãå®è£ ããªããã°ãªããªãã
In implementations that support Document Object Model CSS from DOM Level 2 Views [DOM2Views], every Window object MUST also implement the ViewCSS interface.
http://www.w3.org/TR/Window/#window
ããã«ã ViewCSS 㯠AbstractView ãç¶æ¿ãã¦ããã
A base interface that all views shall derive from.
http://www.w3.org/TR/DOM-Level-2-Views/views.html#Views-AbstractView
AbstractView ã¯ãã¹ã¦ã® view ã®åºåºã¤ã³ã¿ãã§ã¼ã¹ãªã®ã§ã ViewCSS 㯠view ã¨ãããã®ã¨ãããã¨ã«ãªãã
That is, a view is some alternate representation of, or a presentation of, and associated with, a source document.
http://www.w3.org/TR/DOM-Level-2-Views/views.html
view ã¨ã¯å
document ã®è¡¨ç¾ã®ãã¨ãã¤ã¾ãã Window Object 㯠document ã ViewCSS ã§è¡¨ç¾ãããã®ã£ã¦ãã¨ï¼ã ã¨æãï¼ã
document ã¯ããã©ã«ãã® view ã示ãããã« DocumentView ã¨ããã¤ã³ã¿ãã§ã¼ã¹ãå®è£
ãã¦ãããããã¨ãæ¸ãã¦ãã
A Document may implement a DocumentView that has a default view attribute associated with it.
http://www.w3.org/TR/DOM-Level-2-Views/views.html
ã§ã DocumentView ã«ã¯ defaultView ã¨ããããããã£ãå®ç¾©ããã¦ããã
It provides an attribute to retrieve the default view of a document.
// Introduced in DOM Level 2: interface DocumentView { readonly attribute AbstractView defaultView; };http://www.w3.org/TR/DOM-Level-2-Views/views.html#Views-DocumentView
ã¤ã¾ã
Safari 㯠document ã®ããã©ã«ãã® view (defaultView) 㯠ViewCSS ã§ãããã window 㯠view ã§ã¯ãªãã¨ããã¹ã¿ã³ã¹ã§å®è£
ãã¦ããã
IE ã¯ã view ã¨ããæ¦å¿µèªä½ãåå¨ããªãã
Firefox, Opera 㯠document ã®ããã©ã«ãã® view (defaultView) 㯠ViewCSS ã§ãã window ã§ããã
ã¨ãããã¨ããããå®ã«ããããã
getComputedStyle ã§åãããªãã¸ã§ã¯ãã¯ä½ã
å度ãããè¦ã¦ã¿ãã¨
// Introduced in DOM Level 2: interface ViewCSS : views::AbstractView { CSSStyleDeclaration getComputedStyle(in Element elt, in DOMString pseudoElt); };http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-OverrideAndComputed
CSSStyleDeclaration ã¤ã³ã¿ãã§ã¼ã¹ãå®è£
ãããªãã¸ã§ã¯ãã¨ãããã¨ãåããã
ã¡ãªã¿ã«ã CSSStyleDeclaratioin ã¯ã element.style ã¨ããã£ã¦åãããªãã¸ã§ã¯ããå®è£
ãã¦ãã¤ã³ã¿ãã§ã¼ã¹ã¨åãã§ããã
ããã« IE, Opera, Safari, Firefox ã§ã¯ããã®ãªãã¸ã§ã¯ã㯠CSS2Properties ã¨ããã¤ã³ã¿ãã§ã¼ã¹ãå®è£
ãã¦ãã(ã¯ã)ã
CSS2Properties ã®ã¤ã³ã¿ãã§ã¼ã¹ã¯ä»¥ä¸ã®ããã«ãªã£ã¦ãã
// Introduced in DOM Level 2: interface CSS2Properties { attribute DOMString azimuth; attribute DOMString background; attribute DOMString backgroundAttachment; ï¼ ï¼ attribute DOMString width; attribute DOMString wordSpacing; attribute DOMString zIndex; };http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSS2Properties
ãªãã»ã©ãã¤ã¾ãã element.style ã¨ãã£ãã¨ãã®ããã«ç°¡åã«ä½¿ããã£ã¦ãã¨ã§ããã
getComputedStyle ã§åãããªãã¸ã§ã¯ãã¯ãã¤ãããã¯ã
getComputedStyle ã§åãããªãã¸ã§ã¯ãã¯ãã®æã®å¤ã示ãéçãªããã·ã¥ããããã¨ãå
ã®è¦ç´ ã®ç¶æ
ãå¤ããã¨ä¸ç·ã«å¤åãã¦ãããåçãªãªãã¸ã§ã¯ããªã®ããæ°ã«ãªã£ãã
ãããã試ãã¦ã¿ãã¨å
ã®è¦ç´ ã®ç¶æ
ãå¤ããã¨ãä¸ç·ã«å¤åãã¦ããããã¨ãåãã£ãã
ã¤ã¾ãã
<body><p id="target">hoge</p></body>
ã¨ãã html ããã£ãã¨ã
var element = document.getElementById('target'); var style = element.currentStyle || document.defaultView.getComputedStyle(element, ''); alert(style.color); // rgb(0, 0, 0) // ãã㧠<body> ã® color ãå¤ãã document.body.style.color = 'red'; // ã¡ãã㨠<p> ã«ç¶æ¿ããã alert(style.color); // rgb(255, 0, 0)
ã¨ããæãã«ãªãã
getComputedStyle ã§åãããªãã¸ã§ã¯ãã¯ãã¤ãããã¯ã
ï¼
ã¾ã¨ã
ããã§ããªãã ComputedStyle ãã¹ã¿ã¼ï¼