This article example describes how to obtain inline styles by js. Share it for your reference. The specific implementation method is as follows:
Copy the code as follows: <!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 gets inline style</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>
//Get inline style elements
function getStyle( obj , attr )
{
if ( window.getComputedStyle ) {
return getComputedStyle( obj , null )[attr];
}else{
return obj.currentStyle[attr];
}
}
//Get object
function $(id){
return document.getElementById(id);
}
var box = $('box');
//Print style
alert(getStyle(box , 'background-position'));
</script>
I hope this article will be helpful to everyone's JavaScript programming.