简体   繁体   English

使用JavaScript获得CSS属性的价值

[英]Get value of CSS property using JavaScript

I need to do some if logic depending if an element in the DOM has the property display: none . 我需要做一些if逻辑,取决于DOM中的元素是否具有display: none属性display: none

I was looking at getComputedStyle() and have come this far: 我一直在看getComputedStyle()并走到了这么远:

var bla = document.querySelectorAll('#nvk-popup-wapper')[0]
getComputedStyle(bla)

This returns an object and if I expand it in Chrome's console I can see a property display with a value of none . 这将返回一个对象,如果在Chrome的控制台中将其展开,则可以看到一个display值为none的属性。 But if I go bla.display I get undefined whereas I had hoped to get the value of the display property for that element. 但是,如果我去bla.display我将undefined而我希望获得该元素的display属性值。

For a particular element, how do I get the value of the display property for that element? 对于特定元素,如何获取该元素的display属性值?

try this 尝试这个

var style = getComputedStyle(bla)
var display =style.getPropertyValue('display');

Using JQuery : 使用JQuery:

$("#some-element").css("display");

would return the property display of the some-element 将返回some-element的属性display

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM