jQuery .toggle(showOrHide)不起作用 [英] jQuery .toggle( showOrHide ) not working

查看:167
本文介绍了jQuery .toggle(showOrHide)不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用.toggle(showOrHide)函数检查状态.在api文档中,它被称为;

I want to use .toggle( showOrHide ) function to check the status. In the api documentation, it is mentioned as;

$( "#foo" ).toggle( showOrHide );
if ( showOrHide === true ) {
    $( "#foo" ).show();
} else if ( showOrHide === false ) {
    $( "#foo" ).hide();
}

但是它不起作用.有人知道如何使它起作用吗?我想检查其状态是显示还是隐藏.

But its not working. Anybody knows how to make it work? I want to check the status whether its show or hide.

推荐答案

根据文档: http://api.jquery.com/toggle/

该方法的第二个版本接受布尔参数.如果此参数为true,则显示匹配的元素;否则为false.如果为false,则元素被隐藏.本质上,该语句:

The second version of the method accepts a Boolean parameter. If this parameter is true, then the matched elements are shown; if false, the elements are hidden. In essence, the statement:

您需要声明truefalse;

var showOrHide = true;

if ( showOrHide === true ) {
    $( "#foo" ).show();
} else if ( showOrHide === false ) {
    $( "#foo" ).hide();
}

在大多数情况下,使用速记是有意义的,但是OP似乎需要先检查状态.

Using the shorthand would make sense in most cases, but it seems the OP has a requirement to check the status first.

这篇关于jQuery .toggle(showOrHide)不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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