使用JavaScript检查元素CSS显示 [英] Check element CSS display with JavaScript
本文介绍了使用JavaScript检查元素CSS显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以使用JavaScript检查元素的CSS display == block
或 none
?
Is it possible to check if an element's CSS display == block
or none
using JavaScript?
推荐答案
元素有样式
属性,它会告诉你你想要什么,如果样式是内联或使用JavaScript声明的:
Elements have a style
property that will tell you what you want, if the style was declared inline or with JavaScript:
console.log(document.getElementById('someIDThatExists').style.display);
会给你一个字符串值。
如下面sdleihssirhc所述,如果元素的显示
被继承或由CSS规则指定,则需要获取其计算样式:
As sdleihssirhc says below, if the element's display
is being inherited or being specified by a CSS rule, you'll need to get its computed style:
return getComputedStyle(element, null).display;
这篇关于使用JavaScript检查元素CSS显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文