作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想知道如何读取 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/
我是一名优秀的程序员,十分优秀!