js获取内联样式的方法

2019-12-18,,,,,

本文实例讲述了js获取内联样式方法。分享给大家供大家参考。具体实现方法如下:

复制代码 代码如下:<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,target-densitydpi=high-dpi,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
<title>javascript 获取内联样式</title>
</head>

<style>
#box{
width:100px;
height:100px;
background-image:url(1.jpg);
background-size:140% 140%;
background-repeat:no-repeat;
border:20px solid rgba(0,0,0,.3);
background-clip:border-box;
background-position:-20px -20px;
}
</style>
<body>
<div id="box"></div>
</body>
</html>
<script>
//获取内联样式元素
function getStyle( obj , attr )
{
if ( window.getComputedStyle ) {
return getComputedStyle( obj , null )[attr];
}else{
return obj.currentStyle[attr];
}
}
//获取对象
function $(id){
return document.getElementById(id);
}
var box = $('box');

//打印样式
alert(getStyle(box , 'background-position'));
</script>

希望本文所述对大家的javascript程序设计有所帮助。

您可能感兴趣的文章:

  • 获取内联和链接中的样式(js代码)
  • javascript 读取内联之外的样式(style、currentStyle、getComputedStyle区别介绍)
  • 关于javascript获取内联样式与嵌入式样式的实例

《js获取内联样式的方法.doc》

下载本文的Word格式文档,以方便收藏与打印。