JavaScript如何调用CSS属性

JavaScript如何调用CSS属性?


1、JavaScript获取行内style里的样式


(1)、margin、width、left
            obj.style.margin
            obj.style.width
            obj.style.left


(2)、font-size、border-top-width、-moz-user-select
            obj.style.fontSize
            obj.style.borderTopWidth

            obj.style.mozUserSelect


2、JavaScript获取行外部<style></style>里的样式

(1)firefox中用getComputedStyle()方法

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JS获取行外部样式</title>
<style type="text/css">
	#body_div{
		font-size:14px;
		400px;
		height:500px;
	}
</style>
<script type="text/javascript">
	function getStyle()
	{
		var div = document.getElementById("body_div");
		var whole = document.defaultView.getComputedStyle(div,null);
		alert(whole.fontSize);
		alert(whole.width);
		alert(whole.height);
	}
</script>
</head>

<body οnlοad="getStyle();">
   <div id="body_div" style="font-weight:bolder;">JS获取行外部样式</div>
</body>
</html>

(2)IE浏览器用obj.currentStyle方法

原文地址:https://www.cnblogs.com/hzcya1995/p/13314370.html