首页 \ 问答 \ 只有在可见的情况下,jQuery才会隐藏,仅在隐藏时才显示[closed](jQuery hide only if visible, show only if hidden [closed])

只有在可见的情况下,jQuery才会隐藏,仅在隐藏时才显示[closed](jQuery hide only if visible, show only if hidden [closed])

当使用jQuery时,是否有可能隐藏一个div只有当它没有被隐藏时,才显示它,而不是再次向它添加相同的类。

像这样的东西可以用在那里吗?

例如:

$(document).ready(function() {
 $(".trigger").click(function() {
    // Hide it but only if not hidden - hide
    // Later in the script - Show it but only If it's not visible. 
 });
});

<div class="user">Example Div</div> 
<div class="trigger">Load</div>

我已经解决了这个问题。 真实情况有很大不同。

编辑

切换不是我想要做的。 切换将改变一个类。 我强制性地试图隐藏它,但只有当它没有隐藏。 即:只有当隐藏类的当前类未被隐藏时才添加它。

If class != 'hidden' then add the Hidden class

When using jQuery, is it possible to hide a div only if it is not hidden, and show it only if it is not shown, instead of adding the same classes to it again?

Can something like an if be used there?

Eg:

$(document).ready(function() {
 $(".trigger").click(function() {
    // Hide it but only if not hidden - hide
    // Later in the script - Show it but only If it's not visible. 
 });
});

<div class="user">Example Div</div> 
<div class="trigger">Load</div>

I've stripped things down for this question. The real thing is a lot different.

Edit

Toggle is not what I'm trying to do. Toggle will change a class. I'm compulsorily trying to hide it, but only if it's not hidden. That is: add the hidden class only if it's current class is not hidden.

If class != 'hidden' then add the Hidden class

原文:https://stackoverflow.com/questions/19955559
更新时间:2022-11-14 11:11

最满意答案

[namespace.udecl] p10中的这个示例与您的完全相同:

struct B {
   int i;
};
struct X : B {
   using B::i;
   using B::i; // error: double member declaration
};

该错误由[class.mem] p1备份:

成员不应在成员规范中声明两次,除非可以声明嵌套类或成员类模板,然后再定义,除了可以使用opaque-enum-declaration引入枚举 ,稍后使用enum-specifier

所以你走在正确的轨道上。 多个声明都可以,只要它们不破坏其他规则(例如一个定义,成员规范等)

例如,以下内容很好:

struct X;
struct X;

或者更复杂的例子:

struct X 
{
    struct A;
    struct A
    {
       int y;
    };
}; 

struct X;
struct X::A;

This example from [namespace.udecl]p10 is exactly the same as yours:

struct B {
   int i;
};
struct X : B {
   using B::i;
   using B::i; // error: double member declaration
};

The error is backed up by [class.mem]p1:

A member shall not be declared twice in the member-specification, except that a nested class or member class template can be declared and then later defined, and except that an enumeration can be introduced with an opaque-enum-declaration and later redeclared with an enum-specifier.

So you're on the right track. Multiple declarations are OK, as long as they don't break other rules (e.g. one definition, member specifications etc.)

The following, for example, is fine:

struct X;
struct X;

Or a more complex example:

struct X 
{
    struct A;
    struct A
    {
       int y;
    };
}; 

struct X;
struct X::A;

相关问答

更多
  • 很好的问题,有一个复杂的答案。 要真正掌握这一点,您需要彻底了解C ++声明的内部结构。 (请注意,在这个答案中,我将完全省略属性的存在以防止过度复杂)。 声明有两个组成部分:一系列的说明符,后跟一个以逗号分隔的init-declarator列表。 指定符是: 存储类说明符(例如static , extern ) 函数说明符(如virtual , inline ) friend , typedef , constexpr 类型说明符 ,其中包括: 简单类型说明符(例如int , short ) cv-qua ...
  • 为什么在C ++中需要进行声明 编译器希望确保您没有拼写错误或将错误的参数数传递给函数。 所以,它坚持在使用之前首先看到一个“add”(或任何其他类型,类或函数)的声明。 这真的只是允许编译器更好地验证代码,并允许它整理松散的结尾,以便它可以产生一个整洁的目标文件。 如果你不必转发声明的东西,那么编译器会产生一个对象文件,该对象文件必须包含有关可能的函数“add”的所有可能猜测的信息。 链接器必须包含非常聪明的逻辑来尝试并计算出您实际打算调用的“添加”,当“添加”函数可能存在于不同的对象文件中时,链接器与使 ...
  • 在C和C ++中,前向声明非常弱。 它们为编译器提供了一个正式的“承诺”,即如果具有指定签名的函数出现,它将具有您指定的签名。 甚至不保证该函数出现:除非您调用或以其他方式引用声明的函数,否则编译器不会抱怨没有定义的声明。 该标准要求编制者将相同的前向声明视为单一声明。 与根据单一定义规则必须唯一的定义不同 3.2任何翻译单位不得包含任何变量,函数,类类型,枚举类型或模板的多个定义 声明只需要引用相同的定义,即相互等同: 3.3.4给定同一声明区域中的一组声明,每个声明指定相同的非限定名称,它们都应引用同一 ...
  • 不,您只需delete使用new分配的内容。 像int这样的简单值类型永远不需要删除。 如果你的类确实包含了由构造函数或者稍后通过其他方法使用new动态分配的数据,那么析构函数通常应该对所有数据进行解除分配,无论数据是公共的还是私有的。 我可能会补充说,拥有公开可见的动态分配指针成员可能不是最好的设计。 No, you only need to delete that which has been allocated using new. Simple value types like ints never ...
  • 只有两个main原型符合标准的C实现需要识别: int main(void)和int main(int, char *[]) 。 这不是超载,因为每个程序仍然只能有一个main ; 在一个程序中有一个void foo(int, double)在另一个程序中有一个char *foo(FILE *)也不会重载。 这两个原型的原因很方便:一些应用程序需要命令行参数,而其他应用程序则不需要它们。 所有其他原型,如void main(void)和int main(int, char *[], char *[])都是编 ...
  • 您可能对这个问题和答案感兴趣。 关键词:“暂定”。 C99中的暂定定义和链接 You may be interested in this question and the answers. Keywords: "tentative definition". Tentative definitions in C99 and linking
  • 你应该使用这样的代码: re2::RE2::Arg match; re2::RE2::Arg* args[] = { &match }; re2::RE2::FindAndConsumeN(NULL, pattern, args, 1); args将转换为const Arg* args[] 。 内部const没有处理调用代码,它只在FindAndConsumeN 。 不要使用new因为以后无法delete数组。 (用new表示new const re2::RE2::Arg*[] ) You should ...
  • 我认为你要么必须让整个class B成为朋友(它会消除A中的依赖关系,所以它可能是一件好事),或者使用构造函数而不是类内初始化器。 class B; class A { public: A(int i): someNum(i) { } private: int someNum; friend class B; }; class B { public: void someMemberFunction() { /* doStuff */ } private: vect ...
  • [namespace.udecl] p10中的这个示例与您的完全相同: struct B { int i; }; struct X : B { using B::i; using B::i; // error: double member declaration }; 该错误由[class.mem] p1备份: 成员不应在成员规范中声明两次,除非可以声明嵌套类或成员类模板,然后再定义,除了可以使用opaque-enum-declaration引入枚举 ,稍后使用enum-specifie ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。