티스토리 뷰

WS & SCript

currentStyle & getComputedStyle

Ming's 2008. 7. 1. 10:07

인라인이 아닌 외부스타일로 지정하여 값을 읽어오지 못할경우
다음과 같이 해당 속성값을 읽어올 수 있다.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
 <title> New Document </title>
 <meta http-equiv="Content-Type" content="text/html; charset=euc-kr">
 <style type="text/css">
 #ttt {position:absolute; top:30px; left:30px; width:100px; height:100px; background:#ccc;}
 </style>
</head>

<body>

<script type="text/javascript">
<!--
 function getStyle(tid,sStyle) {
  var tg = document.getElementById(tid);
  var val;
  if (tg.currentStyle) { //IE
   val = tg.currentStyle.left;
  } else { //FF
   val = document.defaultView.getComputedStyle(tg, '').getPropertyValue("left");
  }
 
  alert(val);
 }

 window.onload = function() { getStyle('ttt','left'); }
//-->
</script>

<div id="ttt"></div>

</body>
</html>

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2025/08   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
글 보관함