js获取某元素的class里面的css属性值
2014-01-16 16:54
459 查看
js获取某元素的class里面的css属性值代码。
用js如何获取div中css的 margin、padding、height、border等。你可能说可以直接用document.getElementById("id").style.margin获取。但是你说的只能获取直接在标签中写的style的属性,无法获取标签style外的属性(如css文件中的属性)。而下面方法则两者值都可以获取。
js在获取css属性时如果标签中无style则无法直接获取css中的属性,所以需要一个方法可以做到这点。
getStyle(obj,attr) 调用方法说明:obj为对像,attr为属性名必须兼容js中的写法(可以参考:JS可以控制样式的名称写法)。
Js代码:
function getStyle(obj,attr){
var ie = !+"\v1";//简单判断ie6~8
if(attr=="backgroundPosition"){//IE6~8不兼容backgroundPosition写法,识别backgroundPositionX/Y
if(ie){
return obj.currentStyle.backgroundPositionX +" "+obj.currentStyle.backgroundPositionY;
}
}
if(obj.currentStyle){
return obj.currentStyle[attr];
}
else{
return document.defaultView.getComputedStyle(obj,null)[attr];
}
}
完整实例测试代码:
Html代码
<!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=gb2312" />
<title>js获取某元素的class里面的css属性值_www.jbxue.com</title>
<style>
#box1{margin:5px;padding:5px;height:100px;width:200px;}
a{border:1px solid #ccc;border-radius:3px;padding:3px 5px;margin:5px 0;display:inline-block;background:#eee;color:#f60;text-decoration:none;font-size:12px;}
a:hover{color:#ff0000;background:#fff;}
</style>
</head>
<body>
<div id="box1">box1的css.#box1{margin:5px;padding:5px;height:100px;width:200px;}</div>
<a href="javascript:;" onclick="getcss('marginTop')">获取box1的margin-top</a><br />
<a href="javascript:;" onclick="getcss('paddingTop')">获取box1的padding-top</a><br />
<a href="javascript:;" onclick="getcss('height')">获取box1的height</a><br />
<script>
//获取class里面的属性值
var divs=document.getElementById("box1");
function getStyle(obj,attr){
var ie = !+"\v1";//简单判断ie6~8
if(attr=="backgroundPosition"){//IE6~8不兼容backgroundPosition写法,识别backgroundPositionX/Y
if(ie){
return obj.currentStyle.backgroundPositionX +" "+obj.currentStyle.backgroundPositionY;
}
}
if(obj.currentStyle){
return obj.currentStyle[attr];
}
else{
return document.defaultView.getComputedStyle(obj,null)[attr];
}
}
function getcss(typ){
alert(getStyle(divs,typ));
}
</script>
</body>
</html>
用js如何获取div中css的 margin、padding、height、border等。你可能说可以直接用document.getElementById("id").style.margin获取。但是你说的只能获取直接在标签中写的style的属性,无法获取标签style外的属性(如css文件中的属性)。而下面方法则两者值都可以获取。
js在获取css属性时如果标签中无style则无法直接获取css中的属性,所以需要一个方法可以做到这点。
getStyle(obj,attr) 调用方法说明:obj为对像,attr为属性名必须兼容js中的写法(可以参考:JS可以控制样式的名称写法)。
Js代码:
function getStyle(obj,attr){
var ie = !+"\v1";//简单判断ie6~8
if(attr=="backgroundPosition"){//IE6~8不兼容backgroundPosition写法,识别backgroundPositionX/Y
if(ie){
return obj.currentStyle.backgroundPositionX +" "+obj.currentStyle.backgroundPositionY;
}
}
if(obj.currentStyle){
return obj.currentStyle[attr];
}
else{
return document.defaultView.getComputedStyle(obj,null)[attr];
}
}
完整实例测试代码:
Html代码
<!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=gb2312" />
<title>js获取某元素的class里面的css属性值_www.jbxue.com</title>
<style>
#box1{margin:5px;padding:5px;height:100px;width:200px;}
a{border:1px solid #ccc;border-radius:3px;padding:3px 5px;margin:5px 0;display:inline-block;background:#eee;color:#f60;text-decoration:none;font-size:12px;}
a:hover{color:#ff0000;background:#fff;}
</style>
</head>
<body>
<div id="box1">box1的css.#box1{margin:5px;padding:5px;height:100px;width:200px;}</div>
<a href="javascript:;" onclick="getcss('marginTop')">获取box1的margin-top</a><br />
<a href="javascript:;" onclick="getcss('paddingTop')">获取box1的padding-top</a><br />
<a href="javascript:;" onclick="getcss('height')">获取box1的height</a><br />
<script>
//获取class里面的属性值
var divs=document.getElementById("box1");
function getStyle(obj,attr){
var ie = !+"\v1";//简单判断ie6~8
if(attr=="backgroundPosition"){//IE6~8不兼容backgroundPosition写法,识别backgroundPositionX/Y
if(ie){
return obj.currentStyle.backgroundPositionX +" "+obj.currentStyle.backgroundPositionY;
}
}
if(obj.currentStyle){
return obj.currentStyle[attr];
}
else{
return document.defaultView.getComputedStyle(obj,null)[attr];
}
}
function getcss(typ){
alert(getStyle(divs,typ));
}
</script>
</body>
</html>
相关文章推荐
- js获取某元素的class里面的css属性值代码
- js获取某元素的class里面的css属性值代码
- js获取某元素的class里面的css属性值代码(转)
- js获取、设置元素css属性值
- js里面获取相同class的value
- 使用JS获取表单元素里面的内容
- js 一段代码获取元素class样式
- js通过class获取元素
- js获取元素具体样式属性值
- js随笔,css和js禁止网页选择文本,table的class样式使得td的class样式失效,jquery获得元素坐标
- 获取元素css属性值getComputedStyle与*.style.*的区别
- js来获取嵌套的子页面iframe里面的html和form表单中元素值
- js通过getElementById或getElementsByClassName()【】等方式获取元素为null或undefined
- CSS的display属性对js获取元素宽高的影响
- JS获取CSS属性值
- JS原生通过id,class,tagname 来获取元素的方法
- JS 获取元素的属性值,非内联样式
- jQuery/Zepto.js获取datalist元素指定option选项的属性值
- 原生Js获取元素样式属性值的方法
- js获取iframe里面的元素