`

javascript - trick to test visibility of an element

阅读更多

though there is a visible property that you can test if a element is having the visibility set to true (display = none), however, it is more arguable why not just to test the offsetWidth and the offsetHeight property, thought it looks crude, but it shold be incredibly effecient in that if a width and height is 0, then even if the display is not none, you will not be able to see it on the screen.

 

however, there is one exception for the height and width test, it is because that IE has tr return width or height as 0; so we have the following code. 

 

 

<html>
<head>
<title>Visibility Test</title>
<script type="text/javascript">
  function isVisible(elem) {
    var isTR = elem.nodeName.toLowerCase() === "tr",
w = elem.offsetWidth, h = elem.offsetHeight;
    return w !== 0 && h !== 0 && !isTR ?
      true :
      w === 0 && h === 0 && !isTR ?
        false :
        computedStyle(elem, "display") === "none";
  }
  window.onload = function () {
    var block = document.getElementById("block");
    var none = document.getElementById("none");
    // Alerts out 'true'
    alert(isVisible(block));
    // Alerts out 'false'
    alert(isVisible(none));
  };
</script>
</head>
<body>
<div id="block">Test</div>
<div id="none" style="display:none;">Test</div>
</body>
</html>
 

 

分享到:
评论

相关推荐

    python-trick

    Python-trick,上传的事pdf文档

    S-a-D-trick.rar_Join In

    split file and join write in vb6

    深度学习领域CNN橄榄球比赛NFL目标检测(带数据集)-cnn-baseline-more-tta-trick

    训练集视频在train_labels.csv中有相应的标签,而你必须预测的视频在test/文件夹中。今年,我们还为训练和测试集提供了基线头盔检测和分配盒。csv是去年获胜球员分配模型的输出。train_player_tracking.csv在提供的...

    tips-n-trick

    概述 这是Tips-N-Trick,书签等面向公众的文档。

    gtg-grind-trick-generator

    gtg-grind-trick-generator PWA Web应用程序(Node.js,JS,HTML,CSS) Chrome,Safari,Firefox,Edge(Android,iOS,MacOS,Windows) 离线工作Android应用程式使用Google Workbox,Webpack制作

    chinese-nlp-ner-master_chinese_BLSTM_

    一套针对中文实体识别的BLSTM-CRF解决方案,主要包括:数据处理,模型构建,模型训练,模型测试,服务部署(thrift和flask)两种方式。

    py-trick-book:关于 Python 的高级提示和技巧

    Python 提示和技巧 ... git clone https://github.com/plasmashadow/py-trick-book.git 我假设您安装了 ipython 导航到目录并执行 ipython notebook 笔记: 退出间谍活动并自己阅读 或者 保持冷静,加入草帽海贼团

    [Head.First.JavaScript].Michael.Morrison...

    specialist, you’ll be able to take a cluttered room of JavaScript data and impose your will on it with a flurry of virtual labels and storage bins. storing data Your scripts can store data 34 Scripts...

    MicroPython for the Internet of Things-Apress(2017)

    The trick is applying knowledge of technologies to leverage them to the best advantages for your IOT solution. In this book, we explore how to build IOT solutions using an easy-to-understand ...

    21-card-trick:在 React 中完成的 21 张卡片技巧

    React 21 卡技巧一个演示卡片技巧的React应用程序。动机该项目旨在学习如何使用 React 钩子和进行嵌套的 api 调用。 该项目不再进行。怎么玩记住 21 张卡片中的 1 张后,单击完成。 选择您的卡片所在的 3 堆中的哪一...

    MicroPython for the Internet of Things-Apress(2017).pdf

    Internet of Things (IOT) solutions are not nearly as complicated as the name may seem to indicate. Indeed, the IOT is largely another name for what we have already been doing. You may have heard of ...

    HLP-Trick-crx插件

    语言:English (United States) 该扩展程序重新启用了网页上的复制/粘贴功能,上下文菜单和本机突出显示功能。

    MicroPython for the Internet of Things, by Bell 原版PDF

    Internet of Things (IOT) solutions are not nearly as ...how to build IOT solutions using an easy-to-understand programming language named MicroPython running on small, dedicated microcontroller boards.

    CMCC-Trick:招惹CMCC-* WLAN

    Trick-CMCC 利用CMCC公共热点的小漏洞免费上网~~ :) sudo ./conn.sh Notice: 目前只知道我工CMCC有这特色, 其他地区尚不明确 Notice: 脚本适用于使用NetWorkManager网络sds管理工具的系统 Notice: 不必惊讶原理, ...

    Trick

    Trick

    HLP-绝招「HLP-Trick」-crx插件

    该扩展程序重新启用网页上的复制/粘贴功能,上下文菜单和本机突出显示功能。 支持语言:English (United States)

    英文原版-Instant Puppet 3 starter 1st Edition

    Most importantly, it teaches you the trick to get better results every time, by thinking about and expressing your desired outcome in a deterministic fashion.“Instant Puppet 3 Starter” provides you...

    Pro Silverlight2 in CSharp 2008

    It takes a short but definite amount of time, and it’s an obvious inconvenience. However, once the plug-in is installed, the browser can process any content that uses the plug C#.

    HOW TO IMPROVE YOUR BUSINESS LETTERS

    good and simple way to cite your sources is simply to include the name of the author and year of publication in parentheses within the body of your text (Lawrence 1999). Then include the full citation...

    vdr-hattrick-开源

    vdr-hattrick是视频磁盘录像机(VDR)的插件。 它显示了在线足球游戏“ hattrick”(http://www.hattrick.org/)提供的实时比赛。

Global site tag (gtag.js) - Google Analytics