JavaScript如何调用CSS属性

来源:互联网 发布:剑侠情缘手游工资算法 编辑:程序博客网 时间:2024/06/05 09:54

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;width: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 onload="getStyle();">   <div id="body_div" style="font-weight:bolder;">JS获取行外部样式</div></body></html>

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

5 0
原创粉丝点击