獲取非行內的樣式
阿新 • • 發佈:2019-02-11
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style type="text/css">
#div1{
width:100px;
height:100px;
background:yellow;
}
</style>
<script type="text/javascript">
window.onload=function(){
var div1=document.getElementById("div1");
//獲取那個元素
//獲取那個樣式
function getStyle(obj,attr){
if(obj.currentStyle){
return obj.currentStyle[attr];
}else{
return getComputedStyle(obj, false)[attr];
}
}
//alert(getStyle(div1,"width"));
// alert(getStyle(div1,"fontSize"));//獲取預設樣式
//alert(getStyle(div1,"backgroundColor"));
// alert(getStyle(div1,"background"));//IE無法獲取複合樣式
}
</script>
</head>
<body>
<div id="div1" style="font-size: 12px;"></div>
</body>
</html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style type="text/css">
#div1{
width:100px;
height:100px;
background:yellow;
}
</style>
<script type="text/javascript">
window.onload=function(){
var div1=document.getElementById("div1");
//獲取那個元素
//獲取那個樣式
function getStyle(obj,attr){
if(obj.currentStyle){
return obj.currentStyle[attr];
}else{
return getComputedStyle(obj, false)[attr];
}
}
//alert(getStyle(div1,"width"));
// alert(getStyle(div1,"fontSize"));//獲取預設樣式
//alert(getStyle(div1,"backgroundColor"));
// alert(getStyle(div1,"background"));//IE無法獲取複合樣式
}
</script>
</head>
<body>
<div id="div1" style="font-size: 12px;"></div>
</body>
</html>