从元素中获取CSS值

Getting CSS Values from elements

本文关键字:CSS 获取 元素      更新时间:2023-09-26

我需要使用web存储api来存储一个页面上元素的样式变化。我实际上没有线索如何做到这一点,但我想我会开始获得的CSS属性,已更改,并将其存储在一个数组。我试着跟随这里发生的事情,并根据我的问题进行调整。

这是我为了得到值而尝试的,但我不确定它是否正确:

function newItem(){
    var bgImg = document.getElementsByTagName('body').bgImg[0].style.getPropertyValue('background');
    var wideScreen = getElementById('sidebar').style.getPropertyValue('display');
    var playerColor = getElementById('video_container').style.getPropertyValue('background-color');     
    }

我不确定我上面写的代码是否抓住了我需要的信息

您可以使用getComputedStyle().

getComputedStyle()给出了一个元素的所有CSS属性的最终使用值。

var element = document.getElementById('sidebar'),
    style = window.getComputedStyle(element),
    display = style.getPropertyValue('display');

var element = document.getElementById('video_container'),
    style = window.getComputedStyle(element),
    bg = style.getPropertyValue('background-color');