最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Javascript: How to check if element is visible? - Stack Overflow

programmeradmin0浏览0评论

i'm using the lightweight zepto.js framework and now I need to test if an element on the page is visible or not … this my case:

A button triggers the function show_guides().

function show_guides() {
    $('#guides').toggle();

    if ( $('#guides').is(':visible') ) { // does not work
        //$.cookie('guides_visible', 'true');
        console.log("visible");
    } else {
        console.log("invisible");
        //$.cookie('guides_visible', null);
    }
}

If the $('#guides') are visible I want to save a cookie and if they are not I want to get rid of it.

However zepto.js doesn't support selectors like :visible so I have to find a different way. Any ideas how to do that? Right now I'm getting the following error:

Uncaught Error: SYNTAX_ERR: DOM Exception 12

In the zepto documentation i've read this …

For basic support of jQuery’s non-standard pseudo-selectors such as :visible, include the optional “selector” module.

But I have no idea how to include this.

Anybody out the who could help me out here? Thank you in advance.

i'm using the lightweight zepto.js framework and now I need to test if an element on the page is visible or not … this my case:

A button triggers the function show_guides().

function show_guides() {
    $('#guides').toggle();

    if ( $('#guides').is(':visible') ) { // does not work
        //$.cookie('guides_visible', 'true');
        console.log("visible");
    } else {
        console.log("invisible");
        //$.cookie('guides_visible', null);
    }
}

If the $('#guides') are visible I want to save a cookie and if they are not I want to get rid of it.

However zepto.js doesn't support selectors like :visible so I have to find a different way. Any ideas how to do that? Right now I'm getting the following error:

Uncaught Error: SYNTAX_ERR: DOM Exception 12

In the zepto documentation i've read this …

For basic support of jQuery’s non-standard pseudo-selectors such as :visible, include the optional “selector” module.

But I have no idea how to include this.

Anybody out the who could help me out here? Thank you in advance.

Share Improve this question asked Jul 9, 2012 at 8:28 mattmatt 44.4k107 gold badges268 silver badges402 bronze badges 1
  • 2 "But I have no idea how to include this." The same way you load any other JavaScript file. Include this file after you loaded zepto. – Felix Kling Commented Jul 9, 2012 at 8:35
Add a ment  | 

4 Answers 4

Reset to default 3

You can check the display CSS property:

 function show_guides() {

        $('#guides').toggle();

        if ( $('#guides').css('display') == 'block' ) { 
            console.log("visible");
        } else {
            console.log("invisible");
        }
    }

try

     style.display="block";

and

     style.display="hidden";

You can check visibility:visible/hidden, or display:block/none

$('#guides').css('visibility') == 'visible'
$('#guides').css('display') == 'block'

If all you want is to check visibility you can just use this

  function visible(elem){
    elem = $(elem)
    return !!(elem.width() || elem.height()) && elem.css("display") !== "none"
  }

taken straight from the zepto selectors plugin. Otherwise you can just include the selectors module from https://github./madrobby/zepto/blob/master/src/selector.js as Felix Kling suggested

发布评论

评论列表(0)

  1. 暂无评论