jquery选择器问题:仅在隐藏所有元素时执行某些操作 [英] jquery selector question: do something only if all elements are hidden

查看:92
本文介绍了jquery选择器问题:仅在隐藏所有元素时执行某些操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只有当给定ul中的所有li都被隐藏时,我才需要做一些事情。这似乎没有办法。有办法吗?

  if($('some_ul li:hidden')){
// do只有当给定ul中的所有'li'都被隐藏时才会出现问题
}


解决方案

检查返回元素的长度属性。

  if(!$('#some_ul li:visible) ')。length){
//只有当给定ul中的所有'li'都被隐藏时才做某事
}

编辑:隐藏更改为可见






编辑:澄清使用 .length 属性进行布尔测试。 请参阅此答案底部的进一步说明。



$('#some_ul li:visible')。 length 返回找到的元素数。数值等于true或false。



为您提供其真/假值的负数。 / p>

所以如果 $('#some_ul li:visible')。length 找到0个可见元素,那就是与返回 false 相同。



当您放置在它后面,它的布尔值反转为 true 。因此,如果找不到可见元素, if()中的代码将会运行。



与完成相同:

  if($('#some_ul li: visible')。length == 0){
//只有当给定ul中的所有'li'都被隐藏时才做某事
}

...它取数值 $('#some_ul li:visible')。length 然后转动它使用 == 运算符将其转换为布尔值。






To使用:隐藏您需要首先获得总长度,然后将其与隐藏长度进行比较。

  var total = $('#some_ul li'); 

if($('some_ul li:hidden')。length == total){
//只有当给定ul中的所有'li'都被隐藏时才做某事
}






编辑:在回答您的评论时,为了澄清,在测试中有几个值等于true / false。



以下是一些等于false的值的示例:

  var someVariable; // someVariable未定义,等于false 

var someVariable =''; // someVariable是一个空字符串,等于false

var someVariable = false; // someVariable是一个布尔值false,等于false

var someVariable = 0; // someVariable为数字零,等于false

var someVariable = Number('a'); // someVariable是NaN(非数字),等于false

var someVariable = null; // someVariable为null,等于false

以下是一些等于true的示例:

  var someVariable =false; // someVariable是一个字符串,等于true 

var someVariable = 123; // someVariable是一个大于0的数字,等于真

var someVariable = -123; // someVariable是一个小于0的数字,等于true

var someVariable = true; // someVariable是一个布尔值true,等于true

var someVariable =!false; // someVariable是一个否定值,
//否则为false,等于true

如果您对任何值的有效布尔值感到好奇,请在它之前放置 !!

  alert(!! 123); //警告为真

第一个转换与有效布尔值相反的值。第二个将其转换回有效的布尔值。



例如:

  var a; //未定义的变量

alert(a); //提醒undefined,逻辑上等于'false'

alert(!a); //提示未定义的布尔对面,即'true'

alert(!! a); //警告转换器将布尔对面的'true'反馈回'false'


I need to do something only if all the li's in a given ul are hidden.. this doesn't seem to do the trick. is there a way?

if ($('#some_ul li:hidden')) {
  // do something only if all 'li's in a given ul are hidden
}

解决方案

Check the length property of the elements returned.

if ( !$('#some_ul li:visible').length ) {
  // do something only if all 'li's in a given ul are hidden
}

EDIT: changed hidden to visible


EDIT: Clarification on the use of the .length property for a boolean test. Please see further explanation at the bottom of this answer.

$('#some_ul li:visible').length returns the number of elements found. Numeric values equate to true or false.

The ! gives you the negative of its true/false value.

So if $('#some_ul li:visible').length find 0 visible elements, that is just the same as returning false.

When you place ! behind it, it's boolean value is reversed to true. So if no visible elements are found, the code in your if() will run.

It is exactly the same as doing:

if ( $('#some_ul li:visible').length == 0 ) {
  // do something only if all 'li's in a given ul are hidden
}

...which takes the numeric value of $('#some_ul li:visible').length and turns it into a boolean value using the == operator.


To use :hidden you would need to first get the total length, then compare it to the hidden length.

var total = $('#some_ul li');

if ( $('#some_ul li:hidden').length == total ) {
  // do something only if all 'li's in a given ul are hidden
}


EDIT: In response to your comment, for clarification, there are several values that will equate to true/false in a test.

Here are some examples of values that equate to false:

var someVariable;  // someVariable is undefined, equates to false

var someVariable = '';   // someVariable is an empty string, equates to false

var someVariable = false; // someVariable is a boolean false, equates to false

var someVariable = 0;  // someVariable is number zero, equates to false

var someVariable = Number('a');  // someVariable is NaN (Not a Number), equates to false 

var someVariable = null;   // someVariable is null, equates to false

Here are some examples that equate to true:

var someVariable = "false";   // someVariable is a string, equates to true

var someVariable = 123;   // someVariable is a number greater than 0, equates to true

var someVariable = -123;   // someVariable is a number less than 0, equates to true

var someVariable = true;   // someVariable is a boolean true, equates to true

var someVariable = !false;   // someVariable is a negated value that would
                             //    otherwise be false, equates to true

If you're curious about the effective boolean value of any value, place !! before it.

alert( !!123 );   // Alerts true

The first ! converts the value to the opposite of its effective boolean value. The second ! converts the it back to its effective boolean value.

For example:

var a;  // an undefined variable

alert( a );   // alerts undefined, logically equates to 'false'

alert( !a );   // alerts the boolean opposite of undefined, which is 'true'

alert( !!a );   // alerts the converts the boolean opposite 'true' back to 'false'

这篇关于jquery选择器问题:仅在隐藏所有元素时执行某些操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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