我想获得一个css图像规则,比如JavaScript中的(URL,Size),例如Variable。
示例展示:
- HTML右语法:
<div id="mydiv"></div>
- CSS右语法:
<style>
body{
background: url('a.png');
background-image-size: cover;
}
</style>
JS错误语法:
<script>
function GetImgVar(){
var imgsrc = document.body.style.backgroundImage;
document.getElementById('mydiv').style.backgroundImage=imgsrc;
}
</script>
答案 0 :(得分:0)
是的,你可以。试试吧:
<style id="stylesheet1">
body{
background: url('a.png');
background-image-size: cover;
}
</style>
<script>
var sheet = document.querySelector('#stylesheet1').sheet;
console.log(sheet.cssRules);// This will print all css rules that belongs to the body tag;
</script>