2011-09-06 6 views
3

javascript를 사용하여 HTML 요소의 style 속성 값을 인쇄하는 방법은 무엇입니까? 나는 propertywidth 같은 것입니다 document.getElementById('myId').style.property를 사용하여 특정 스타일 속성 값을 얻을 수 height자바 스크립트를 사용하여 HTML 요소의 인라인 스타일 값 인쇄

그러나

, 내가 요소에 대한 스타일의 전체 목록을 얻을 수있는 방법?

+0

있어 ** (즉, "인라인"스타일) 그런 다음 유일한 방법은 * getAttribute * (일부 브라우저에서는 버그가 있으므로 신뢰할 수 없음)입니다. 그러나 HTML 요소의 스타일 객체에서 다양한 ** 속성 ** 값을 원하면 아래의 답변을 참조하십시오. – RobG

답변

2

String으로 document.getElementById('myId').style.cssText, 또는 document.getElementById('myId').style을.

편집 :

는 지금까지 내가 말할 수있는, 이것은 "실제", 인라인 스타일을 반환합니다. <a id='myId' style='font-size:inherit;'> 요소에서 document.getElementById('myId').style.cssText"font-size:inherit;"을 반환해야합니다. 원하는 내용이 아닌 경우 document.defaultView.getComputedStyle 또는 document.getElementById('myId').currentStyle으로 시도하십시오. 첫 번째 것은 모두 IE를 제외하고 두 번째 것은 IE 전용입니다. 계산 된 대 계단식 스타일에 대한 자세한 내용은 here을 참조하십시오.

+0

실제로 문자열로 도움이 필요합니다. –

1

이 개체의 덤프를 수행해야합니다 은 여기 Example

편집이다 : 그것 좀 이상을 : Object로서

for (var prop in styles) { 
    console.log(styles[prop], styles[styles[prop]]); 
} 
+0

나는 똑같은 생각을했다. 그러나 불행히도, 그것은 작동하지 않습니다. – NT3RP

+1

콘솔 개체 란 무엇입니까? –

+0

https://developer.mozilla.org/en/Using_the_Web_Console – Joe

1
<div id="x" style="font-size:15px">a</div> 
<script type="text/javascript"> 
function getStyle(oElm, strCssRule){ 
    var strValue = ""; 
    if(document.defaultView && document.defaultView.getComputedStyle){ 
     strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule); 
    } 
    else if(oElm.currentStyle){ 
     strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){ 
      return p1.toUpperCase(); 
     }); 
     strValue = oElm.currentStyle[strCssRule]; 
    } 
    return strValue; 
} 

// get what style rule you want 
alert(getStyle(document.getElementById('x'), 'font-size')); 
</script> 
+0

document.defaultView는 IE 9에서 지원됩니다. –

+0

아니요,하지만''currentStyle' ' –

관련 문제