无法返回HTML元素的css属性 [英] cannot return css property of HTML element

查看:89
本文介绍了无法返回HTML元素的css属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HTML中有元素:< div id =block>< / div> ,它的属性是通过CSS:

There is element in HTML: <div id="block"></div>, and it's property via CSS:

#block { 
    background: red;
 }

如果我想在JavaScript中为一些变量分配CSS中的属性,比如这个:

If I want to assign in JavaScript to some variable a property from CSS, like this:

var value = document.getElementById('block').style.background // It doesn't work

如何解决这个问题?

推荐答案

您可以使用 getComputedStyle

You can use getComputedStyle:

var value = window.getComputedStyle(document.getElementById('block')).getPropertyValue("background-color");
console.log(value);

这篇关于无法返回HTML元素的css属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆