查找元素是否可见(JavaScript) [英] Finding if element is visible (JavaScript )
问题描述
我已经尝试获取元素显示属性值,并查找元素ID是否可见,但两者都没有工作。当我尝试.getAttribute它总是返回null;我不知道为什么,因为我知道id被定义并且具有显示属性。
这是我尝试过的两种不同方法的代码:
var myvar = $(#mydivID)。is(:visible);
var myvar = document.getElementById(mydivID)。getAttribute(display);
非常感谢任何指导或帮助。
显示不是属性,它是样式
属性中的CSS属性。
您可能会查找
var myvar = document.getElementById(mydivID)。style.display ;
或
var myvar = $(#mydivID)。css('display');
I have a javascript function that tries to determine whether a div is visible and does various processes with that variable. I am successfully able to swap an elements visibility by changing it's display between none and block; but I cannot store this value...
I have tried getting the elements display attribute value and finding if the the element ID is visible but neither has worked. When I try .getAttribute it always returns null; I am not sure why because I know that id is defined and it has a display attribute.
Here is the code of the two different methods I have tried:
var myvar = $("#mydivID").is(":visible");
var myvar = document.getElementById("mydivID").getAttribute("display");
Any guidance or assistance would be greatly appreciated.
Display is not an attribute, it's a CSS property inside the style
attribute.
You may be lookig for
var myvar = document.getElementById("mydivID").style.display;
or
var myvar = $("#mydivID").css('display');
这篇关于查找元素是否可见(JavaScript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!