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

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


当前回答

演示式链接

$( 点击“ 点击' ” ) 。 点击( 函数 ) { $( 书 ) { $( 书 ) } 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 查看元素是否隐藏或可见 ?

其他回答

有很多方法可以检查元素是否可见或隐藏在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/

用于检查元素是否可见、不显示,甚至不透明度水平的扩展功能

如果元素不可见, 它返回错误 。

function checkVisible(e) {
    if (!(e instanceof Element)) throw Error('not an Element');
    const elementStyle = getComputedStyle(e);
    if (elementStyle.display === 'none' || elementStyle.visibility !== 'visible' || elementStyle.opacity < 0.1) return false;
    if (e.offsetWidth + e.offsetHeight + e.getBoundingClientRect().height +
        e.getBoundingClientRect().width === 0) {
        return false;
    }
    const elemCenter   = {
        x: e.getBoundingClientRect().left + e.offsetWidth / 2,
        y: e.getBoundingClientRect().top + e.offsetHeight / 2
    };
    if (elemCenter.x < 0 || elemCenter.y < 0) return false;
    if (elemCenter.x > (document.documentElement.clientWidth || window.innerWidth)) return false;
    if (elemCenter.y > (document.documentElement.clientHeight || window.innerHeight)) return false;
    let pointContainer = document.elementFromPoint(elemCenter.x, elemCenter.y);
    do {
        if (pointContainer === e) return true;
    } while (pointContainer = pointContainer.parentNode);
    return false;
}
expect($("#message_div").css("display")).toBe("none");

这是用来检查标签是否可见的选项

/ 使用纯 cs 选择器 { 提醒 { { / 使用jquery 过滤器 () 的方法 { / 使用 jquery's is () 方法 { { 警报 ( 段落可见 ( 使用 () 方法 ) } } ; / 使用 jquery 过滤器 () 的方法 $ ( p) 过滤器 (: 可见) { 提醒 ( 段落可见 ( 使用 () 过滤器 () 方法 检查) } } ; / / 您可以使用 hidden i 。

可以创建函数以检查可见度/显示属性,以测量元素是否显示在 ui 中。

function checkUIElementVisible(element) {
    return ((element.css('display') !== 'none') && (element.css('visibility') !== 'hidden'));
}

工作小儿