JavaScript如何调用CSS属性
2015-10-12 20:43
597 查看
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方法
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方法
相关文章推荐
- javascript库之Mustache库使用说明
- Javascript 面向对象编程(一):封装
- [smarty] 在smarty模板中使用smarty变量初始化 javascript 变量的问题
- JavaScript开发工具WebStorm教程:用户界面简介
- JS window事件全集解析
- js控制select数据绑定下拉列表
- JSTL 动态生成表格
- js 不支持 长数字 decimal 会丢失
- 开始用JavaScript写一些小东西
- BZOJ1029: [JSOI2007]建筑抢修
- JS添加可信站点、修改ActiveX安全设置,禁用弹出窗口阻止程序的方法
- js 回调(callback)
- swiper.js——初识
- js url传值中文乱码之解决之道
- Json解析
- 通过JavaScript函数向网页中输出数据
- 【JavaScript】轻易改变的背景和字体颜色页面
- MVC中Json的使用:Controller中Json的处理
- 转: .NET MVC3 几种返回 JSON 对象的方式和注意事项
- jsf初学解决faces 中文输入乱码问题