2011-01-11 5 views
0

자바 스크립트에서 CSS 속성을 어떻게 얻을 수 있습니까?자바 스크립트의 인라인 CSS 값

예 : 경고에

<style> 
    #body{ 
     background:red; 
     } 
    </style> 

    <script> 
     function valid(form){ 
      alert('enter'); 
      var test = document.getElementById('body').style.background ; 
      alert(test'); 
     } 
    </script> 
</head> 
<body id="body"> 

나는 배경 색상을 얻을 수 아니에요!

+0

도움이 될 수 있습니다. http://www.wpdfd.com/forums/wpdfd/scripting/retrieving_css_values_via_javascript/ – Egalitarian

답변

6

요소의 style 속성은 스타일 규칙에 의해 적용된 것이 아니라 요소 자체의 스타일 정보 (예 : style 속성을 통해)를 반영합니다. 사람들을 얻으려면, 당신이 필요로하는 것 getComputedStyle :

var foo = document.getElementById('foo'); 
display("foo's background color is: " + 
     window.getComputedStyle(foo, null).getPropertyValue('background-color')); 

Live example


오프 주제 :이 물건의 일부는 jQuery 같은 라이브러리, Prototype, YUI, Closure에 의해 쉽게 구성되어, 또는 any of several others.

+0

감사합니다. – Ani

+0

나는 16 진수를 얻는 방법을 rgb에 알 렸습니다. – Ani

+0

자바 스크립트를 통해 기존 값을 변경하는 방법은 무엇입니까? – Ani