如何使用 . hide () 、.show () 或.toggle () 来切换元素的可见度 ?

如果元素可见或隐藏, 我如何测试 ?


当前回答

这些答案都没有涉及我所理解的问题, 也就是我正在寻找的问题, “我如何处理有可见度的项目:隐藏的?” 。 无论是: 可见的还是: 隐藏的, 都无法处理, 因为两者都在寻找文档中的显示。 据我所知, 没有选择器可以处理 cs 可见度。 这就是我是如何解决这个问题的( 标准拼写选择器, 可能有更压缩的语法 ) :

$(".item").each(function() {
    if ($(this).css("visibility") == "hidden") {
        // handle non visible state
    } else {
        // handle visible state
    }
});

其他回答

您也可以使用普通的笔记本进行此项操作 :

function isRendered(domObj) {
    if ((domObj.nodeType != 1) || (domObj == document.body)) {
        return true;
    }
    if (domObj.currentStyle && domObj.currentStyle["display"] != "none" && domObj.currentStyle["visibility"] != "hidden") {
        return isRendered(domObj.parentNode);
    } else if (window.getComputedStyle) {
        var cs = document.defaultView.getComputedStyle(domObj, null);
        if (cs.getPropertyValue("display") != "none" && cs.getPropertyValue("visibility") != "hidden") {
            return isRendered(domObj.parentNode);
        }
    }
    return false;
}

注:

各地工作 , 嵌套元素为 css 工作, 内嵌样式不需要框架 。

演示式链接

$( 点击“ 点击' ” ) 。 点击( 函数 ) { $( 书 ) { $( 书 ) } toggle (' slow' , 函数 () {/ // 动画完成 ) 。 提醒 ($( 书 ) . is ( : : 可以看到 ) ); / lt; - - 如果隐藏, 如果可见假 ) , 则真实 ; } ; ; & lt; 鼠标; 标 src= "https:// upload. wikimedia. org/ wikipedia/ commons/8/ 87/ chrome_ icon_ 282011. 29. png" alt=" 宽度= 300"/ & gt; https://upload. / & gt;

来源(来自我的博客):

博客加插 nplay - jquery 工具和部件: 如何使用 jquery 查看元素是否隐藏或可见 ?

毕竟,没有一个例子适合我, 所以我写了我自己的。

测试( 不支持互联网探索过滤器: alpha) :

a) 检查文档是否隐藏

b) 检查元素是否为零宽度/高度/不透明或显示:无/可见度:内嵌样式中隐藏

c) 检查元素的中心(也因为它比测试每个像素/角的速度快)是否被其他元素(和所有祖先,例如:溢出:隐藏/滚动/一个元素在另一个元素之上)或屏幕边缘所隐藏

d) 检查元素是否为零宽度/高度/不透明或显示:无/可见度:以计算样式隐藏(在所有祖先中)

测试在

和机器人4.4(本地浏览器/铬/火狐)、火狐(风窗/摩克)、铬(风窗/摩克)、歌剧(风窗预示/摩克 Webkit)、互联网探索者(互联网探索者5-11文档模式+虚拟机器上的互联网探索者8)和Safari(风窗/摩克/奥兹)。

var is_visible = (function () {
    var x = window.pageXOffset ? window.pageXOffset + window.innerWidth - 1 : 0,
        y = window.pageYOffset ? window.pageYOffset + window.innerHeight - 1 : 0,
        relative = !!((!x && !y) || !document.elementFromPoint(x, y));
        function inside(child, parent) {
            while(child){
                if (child === parent) return true;
                child = child.parentNode;
            }
        return false;
    };
    return function (elem) {
        if (
            document.hidden ||
            elem.offsetWidth==0 ||
            elem.offsetHeight==0 ||
            elem.style.visibility=='hidden' ||
            elem.style.display=='none' ||
            elem.style.opacity===0
        ) return false;
        var rect = elem.getBoundingClientRect();
        if (relative) {
            if (!inside(document.elementFromPoint(rect.left + elem.offsetWidth/2, rect.top + elem.offsetHeight/2),elem)) return false;
        } else if (
            !inside(document.elementFromPoint(rect.left + elem.offsetWidth/2 + window.pageXOffset, rect.top + elem.offsetHeight/2 + window.pageYOffset), elem) ||
            (
                rect.top + elem.offsetHeight/2 < 0 ||
                rect.left + elem.offsetWidth/2 < 0 ||
                rect.bottom - elem.offsetHeight/2 > (window.innerHeight || document.documentElement.clientHeight) ||
                rect.right - elem.offsetWidth/2 > (window.innerWidth || document.documentElement.clientWidth)
            )
        ) return false;
        if (window.getComputedStyle || elem.currentStyle) {
            var el = elem,
                comp = null;
            while (el) {
                if (el === document) {break;} else if(!el.parentNode) return false;
                comp = window.getComputedStyle ? window.getComputedStyle(el, null) : el.currentStyle;
                if (comp && (comp.visibility=='hidden' || comp.display == 'none' || (typeof comp.opacity !=='undefined' && comp.opacity != 1))) return false;
                el = el.parentNode;
            }
        }
        return true;
    }
})();

如何使用 :

is_visible(elem) // boolean

您可以使用此功能:

$(element).is(':visible');

示例代码

$( document) {( pready) {( ) {( ) {( ) $( ” tggle” ) . click () {( ) {( ) {( ) {( ) {( ) {( ) $( ) ) {( ) $( ) ) } . 点击 ( ) {( ) {( ) {( ” ) } } } } ; & ; 标注 src=" https://ajaax.googleapis.com/ax/libs/ jquery/ 12.2/ query. min. js&gt; & () / statict&gt; & pl= "content" & gt; this is a

这是内部拼写如何解决这个问题的:

jQuery.expr.pseudos.visible = function( elem ) {
    return !!( elem.offsetWidth || elem.offsetHeight || elem.getClientRects().length );
};

如果您不使用 jquery, 你可以只是利用这个代码 并把它变成你自己的功能:

function isVisible(elem) {
    return !!( elem.offsetWidth || elem.offsetHeight || elem.getClientRects().length );
};

只要元素是可见的, 便会返回真实状态 。