简体   繁体   English

检查元件 CSS 显示与 JavaScript

[英]Check element CSS display with JavaScript

Is it possible to check if an element's CSS display == block or none using JavaScript?是否可以使用 JavaScript 检查元素的 CSS 是否display == blocknone

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 :正如下面 sdleihssirhc 所说,如果元素的display被继承或由 CSS 规则指定,则需要获取其计算样式

return window.getComputedStyle(element, null).display;

Elements have a style property that will tell you what you want, if the style was declared inline or with JavaScript:元素有一个style属性,它会告诉你你想要什么,如果样式是内联声明的或使用 JavaScript 声明的:

console.log(document.getElementById('someIDThatExists').style.display);

will give you a string value.会给你一个字符串值。

If the style was declared inline or with JavaScript, you can just get at the style object:如果样式是内联声明的或使用 JavaScript 声明的,您可以直接获取style对象:

return element.style.display === 'block';

Otherwise, you'll have to get the computed style, and there are browser inconsistencies.否则,您将不得不获取计算样式,并且会出现浏览器不一致的情况。 IE uses a simple currentStyle object, but everyone else uses a method: IE 使用一个简单的currentStyle对象,但其他人都使用一个方法:

return element.currentStyle ? element.currentStyle.display :
                              getComputedStyle(element, null).display;

The null was required in Firefox version 3 and below. Firefox 3 及以下版本需要null

For jQuery, do you mean like this?对于jQuery,你的意思是这样吗?

$('#object').css('display');

You can check it like this:你可以这样检查:

if($('#object').css('display') === 'block')
{
    //do something
}
else
{
    //something else
}

This answer is not exactly what you want, but it might be useful in some cases.这个答案并不完全是您想要的,但在某些情况下可能有用。 If you know the element has some dimensions when displayed, you can also use this:如果你知道元素在显示时有一些尺寸,你也可以使用这个:

var hasDisplayNone = (element.offsetHeight === 0 && element.offsetWidth === 0);

EDIT: Why this might be better than direct check of CSS display property?编辑:为什么这可能比直接检查 CSS display属性更好? Because you do not need to check all parent elements.因为您不需要检查所有父元素。 If some parent element has display: none , its children are hidden too but still has element.style.display !== 'none' .如果某个父元素有display: none ,它的子元素也被隐藏了,但仍然有element.style.display !== 'none'

是的。

var displayValue = document.getElementById('yourid').style.display;

Basic JavaScript:基本 JavaScript:

if (document.getElementById("elementId").style.display == 'block') { 
  alert('this Element is block'); 
}

To find out if it's visible with plain JavaScript, check whether the display property is 'none' (don't check for 'block', it could also be blank or 'inline' and still be visible):要确定它是否在纯 JavaScript 中可见,请检查 display 属性是否为 'none'(不要检查 'block',它也可能是空白或 'inline' 并且仍然可见):

var isVisible = (elt.style.display != "none");

If you are using jQuery, you can use this instead:如果您使用的是 jQuery,则可以使用以下代码:

var isVisible = $elt.is(":visible");

With pure javascript you can check the style.display property.使用纯 javascript,您可以检查style.display属性。 With jQuery you can use $('#id').css('display')使用 jQuery,您可以使用$('#id').css('display')

I just found the function element.checkVisibility() which is returning false when display: none is set.我刚刚发现 function element.checkVisibility()display: none设置时返回false

Honestly, I cannot find any documentation for it, so I do not know if it is Chrome specific.老实说,我找不到任何文档,所以我不知道它是否特定于 Chrome。

You can check it with for example jQuery:您可以使用例如 jQuery 来检查它:

$("#elementID").css('display');

It will return string with information about display property of this element.它将返回字符串,其中包含有关此元素的显示属性的信息。

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

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