【问题标题】:How to know the status of the div in jquery?如何知道jquery中div的状态?
【发布时间】:2011-08-06 02:20:59
【问题描述】:

我正在使用jquery 开发一个应用程序。我想知道div 的状态,无论div 是显示状态还是隐藏状态 像这样:

if($("#test").show()==true) 
{
//some operration
}
else
{
//some operration
}

alert($("#test").show()==true); 始终显示false

请帮帮我...

【问题讨论】:

    标签: jquery html hide show status


    【解决方案1】:

    您可以使用 is() 和 :visible 选择器。

    if( $('#test').is(':visible') ) { ... }
    

    【讨论】:

      【解决方案2】:

      试试

      $(element).is(":visible") 
      

      Reference
      注意hidden fails on elements that have width but no height.

      【讨论】:

        【解决方案3】:

        is(':visible') 当然是正确的。

        在我几乎所有的 jQuery 应用程序中,我都引入了一个简单的插件 isVisible

        $.fn.isVisible = function() {
            return $.expr.filters.visible(this[0]);
        };
        

        对于完全相同的功能,这比上述函数 (jsPerf example) 快大约 50 倍。

        if ($('#yourElement').isVisible()) {
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-10-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-30
          相关资源
          最近更新 更多