[英]Can I Get CSS Image Rule in JavaScript?
我想获得一个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>
是的你可以。 试试吧:
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.