gpt4 book ai didi

javascript - 如何从 JavaScript 读取 CSS 值?

转载 作者:行者123 更新时间:2023-12-03 09:59:55 25 4
gpt4 key购买 nike

我想知道如何读取 HTML 元素的 CSS 值并将其保存到变量中。例如说你有这个:

<tr id="presentationProperty" style="display: none;">

我希望能够读取 display: none 属性并将其保存到 JavaScript 中的一个变量中。这可能吗?如果可能,如何

最佳答案

简单:

var display = document.getElementById('presentationProperty').style.display;

在全局范围内,您可以像这样访问元素的样式属性:

var getStyle = function(elementID, prop) {
return document.getElementById(''+elementID+'').style[prop];
};

alert (getStyle('presentationProperty', 'display')); // block
alert (getStyle('presentationProperty', 'position'));
alert (getStyle('presentationProperty', 'backgroundColor'));
// ...

关于javascript - 如何从 JavaScript 读取 CSS 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22173689/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com