只有在可见的情况下,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
最满意答案
[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语言之后还可以学什么[2022-05-01]
c++ -
C ++中的声明(Declarations in C++)[2023-12-24]
很好的问题,有一个复杂的答案。 要真正掌握这一点,您需要彻底了解C ++声明的内部结构。 (请注意,在这个答案中,我将完全省略属性的存在以防止过度复杂)。 声明有两个组成部分:一系列的说明符,后跟一个以逗号分隔的init-declarator列表。 指定符是: 存储类说明符(例如static , extern ) 函数说明符(如virtual , inline ) friend , typedef , constexpr 类型说明符 ,其中包括: 简单类型说明符(例如int , short ) cv-qua ... -
为什么在C ++中需要进行声明 编译器希望确保您没有拼写错误或将错误的参数数传递给函数。 所以,它坚持在使用之前首先看到一个“add”(或任何其他类型,类或函数)的声明。 这真的只是允许编译器更好地验证代码,并允许它整理松散的结尾,以便它可以产生一个整洁的目标文件。 如果你不必转发声明的东西,那么编译器会产生一个对象文件,该对象文件必须包含有关可能的函数“add”的所有可能猜测的信息。 链接器必须包含非常聪明的逻辑来尝试并计算出您实际打算调用的“添加”,当“添加”函数可能存在于不同的对象文件中时,链接器与使 ...
-
C ++函数声明(C++ function declarations)[2022-03-17]
在C和C ++中,前向声明非常弱。 它们为编译器提供了一个正式的“承诺”,即如果具有指定签名的函数出现,它将具有您指定的签名。 甚至不保证该函数出现:除非您调用或以其他方式引用声明的函数,否则编译器不会抱怨没有定义的声明。 该标准要求编制者将相同的前向声明视为单一声明。 与根据单一定义规则必须唯一的定义不同 3.2任何翻译单位不得包含任何变量,函数,类类型,枚举类型或模板的多个定义 声明只需要引用相同的定义,即相互等同: 3.3.4给定同一声明区域中的一组声明,每个声明指定相同的非限定名称,它们都应引用同一 ... -
C ++:删除公共和私有中的所有声明是否正确?(C++: is it right to delete all declarations in public and private?)[2023-07-23]
不,您只需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 *[])都是编 ...
-
多个声明和定义(Multiple declarations and definitions)[2022-05-18]
您可能对这个问题和答案感兴趣。 关键词:“暂定”。 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 ...
-
C ++中的多个声明(Multiple Declarations in C++)[2023-09-01]
[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 ...