function _style(elem, property, value) { elem = elem || document.documentElement; property = property || ''; value = value || ''; if (!value) { try { // IE or 표준 브라우저 // IE의 경우 함수에 전달된 style 속성인 "backgroundColor" 를 사용해도 무방(에러가 나지 않음)하지만, // 그 외 표준 브라우저인 경우는 다르다. // 즉 아래 코드와 같이 전달된 property 속성을 "background-color" 형식으로 치환해 사용해야 에러가 발생하지 않는다. // backgroundColor --> background-color 치환 정규식: property.replace(/([A-Z]+)/g, '-$1') return elem.currentStyle && elem.currentStyle[property] || document.defaultView && document.defaultView.getComputedStyle && document.defaultView.getComputedStyle(elem, null).getPropertyValue(property.replace(/([A-Z]+)/g, '-$1')) || ''; } catch (e) { return null; } } else { if (elem.style) elem.style[property] = value; return elem; } } console.log(_style(document.body, 'backgroundColor', 'red')); console.log(_style(document.body, 'backgroundColor', 'gray'));