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

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


当前回答

由于问题只涉及一个要素,本守则可能更适合:

// Checks CSS content for display:[none|block], ignores visibility:[true|false]
$(element).is(":visible");

// The same works with hidden
$(element).is(":hidden");

它与twern的建议相同,但适用于一个单一要素;它与jquery faq中推荐的算法相符。

我们用 jquery's is () 来检查选中元素与其它元素、 选择器或任何 jquery 对象的对比。 此方法沿 dom 元素绕行查找匹配, 满足已传递参数 。 如果匹配, 返回为真, 否则返回为假 。

其他回答

要检查是否显示 I 使用 :

if ( !$('#book').is(':visible')) {
    alert('#book is not visible')
}

或以下是 sam , 将jquery 选择器保存在一个变量中, 以便在需要它多次时有更好的性能 :

var $book = $('#book')

if(!$book.is(':visible')) {
    alert('#book is not visible')
}

只需通过检查布林值来检查可见性, 例如 :

if (this.hidden === false) {
    // Your code
}

i 为每个函数使用此代码。 否则您可以使用( “: 可见” ) 来检查元素的可见度 。

$( document). ready( { var discrible () { var revision = $ (' retemintion' ). is (': visible' ) ; 如果 (visible) { 备注 (“ 可见”) { 备注 ” ; // code} 其它 { 备警 (“ hidden” ) ;}} } ; & lt; 标注 src= "https://code. jquery. com/jquery-1.10.2. js; & gt;/ statimt>

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

测试( 不支持互联网探索过滤器: 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

有很多方法可以检查元素是否可见或隐藏在jquery中。

demo demo html 例如引用

<div id="content">Content</div>
<div id="content2" style="display:none">Content2</div>

使用可见度过滤选择器$( 元素: 隐藏) 或$( 元素: 可见)

$( 元素: 隐藏 ) : 选择隐藏的所有元素 。 例如 : $( 内容 2: 隐藏 ) show (); $( 元素: 可见 ) : 选择可见的所有元素 。 例如 : $( 内容: 可见 ) . css ( “ 颜色 ” , “ ee ” ) ;

见http://api.jquery.com/类/选择者/visibility-filter-selectors/

正在过滤

    Example:
       $('#content').is(":visible").css('color', '#EEE');

    Or checking condition
    if ($('#content').is(":visible")) {
         // Perform action
    }

见http://api.jquery.com/is/