如何确定jQuery toggle()的当前状态 [英] How to determine the current state of jQuery toggle()

查看:321
本文介绍了如何确定jQuery toggle()的当前状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当另一个div被点击时,我显示并隐藏了div。我需要能够确定一个特定的div是显示还是隐藏。



最终我要做的是,当某个div被点击时,所有其他显示/隐藏div应该被隐藏,然后确定clicked-div的相关div是否显示,如果隐藏它,否则显示它。



我也需要添加/删除一个css类(用于背景颜色)到显示/隐藏div基于其切换状态。



这里是我用来切换的jQuery代码显示/隐藏状态的div:

  $('#movieListTable')。on('click','div。 vitalsTable',函数(e){

//如果点击编辑按钮或删除复选框,则不执行任何操作
if(event.target.className!=='btnEditMovie'& amp; ;& event.target.className!=='chkDeleteMovie'){

$(this).parent()。parent()。find('div.detailsTable')。toggle('blind ','easeInOutQuart',300);
}
});

奇怪而令人沮丧的是,我读过的所有内容,包括SO的答案,表示我应该只是能够检查显示状态(无)或可见属性,但在控制台中检查这些都没有设置在显示/隐藏的div上。



我为您创建了一个 jsFiddle



关于toggle()的 jQuery文档讨论了在页面的3/4处,使用toggle()方法的布尔参数showOrHide,但我一直无法弄清楚如何使用它自己...

$您可以使用 $(div).is(':visible') p>

I'm showing and hiding divs when another div is clicked on. I need to be able to determine if a particular div is either shown or hidden.

Ultimately what I'm trying to do is, when a certain div is clicked on, all other "show/hide" divs should be hidden and then determine if the clicked-div's related div is showing, if so hide it, otherwise show it.

I also need to add/remove a css class (for background color) to the shown/hidden div based on its toggle state.

Here's the jQuery code I'm using to toggle the show/hide state of the divs:

$('#movieListTable').on('click', 'div.vitalsTable', function (e) {

    // Don't do anything if the Edit button or Delete checkbox is clicked
    if (event.target.className !== 'btnEditMovie' && event.target.className !== 'chkDeleteMovie') {

        $(this).parent().parent().find('div.detailsTable').toggle('blind','easeInOutQuart', 300);
    }
});

The weird - and frustrating - thing is, everything I've read, including answers here at SO, indicate I should just be able to either check for the display state ("none") or the visible property, but checking in the console neither of those are being set on the div that is being shown/hidden.

I created a jsFiddle for you to mess with.

The jQuery documentation on toggle() discusses, about 3/4 of the way down the page, a boolean parameter "showOrHide" for one of the ways to use toggle(), but I haven't been able to figure out how to use that myself...

解决方案

you can use $(div).is(':visible')

这篇关于如何确定jQuery toggle()的当前状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆