如何检查 jQuery 查找返回 true 或 false
How to check jQuery find return true or false?
HTML Code
<div class="parent1">
<div class="child1">Child1</div>
<div class="child2">Child2</div>
<div class="child3">Child3</div>
</div>
<div class="parent2">
<div class="child1">Child1</div>
<div class="child2">Child2</div>
</div>
j查询代码
alert($(".parent1").find(".child3").text());
alert($(".parent2").find(".child3").text());
我的问题是如何检查find
函数返回真或假?
在上面的代码中,它返回空白值,其中parent1
类child3
类parent2
类没有类child3
类。
JS小提琴
你不能只使用find
if
条件。可以使用has
或检查length
属性。
var elm = $('.parent1');
if(elm.has('.child3')){
var child3 = elm.find('.child3');
}
或者只是像这样
var child3 = $('.parent1').find(".child3");
if(child3.length > 0) {
// child3 is present
}
可以使用.length
属性来检查元素是否存在。
var elem = $(".parent2").find(".child3");
if(elem.length){
alert(elem.text());
}else{
alert('Element doesnt exists')
}
或者,您可以使用.has()
var elem = $(".parent2");
if(elem.has(".child3")){
alert(elem.find(".child3").text());
}else{
alert('Element doesnt exists')
}
正如find()
文档所解释的那样,find()
返回一个jQuery对象。它有一个长度属性,可以检查查询是否成功。
if($(".parent1").find(".child3").length > 0) {
alert("parent1 child3");
}
if($(".parent2").find(".child3").length > 0) {
alert("parent2 child3");
}
每当将选择器包装在$()
时,它将始终返回一个包含匹配元素数组的 jQuery 对象。
因此,您可以使用 length
属性来测试是否存在匹配项
var $collection = $(".parent2").find(".child3").length;
if ($collection.length)....
您也可以使用其他方法,例如is()
var #collection = $(".parent2");
if($collection.children().is(".child3") /* returns tru if there are matches
.parent2 没有元素 .child3,
alert($(".parent2").find(".child3").text());
试试这个:
alert($(".parent2").find(".child2").text());
如果您只想使用最后一项,请尝试此操作
alert($(".parent1").find("[class^=child]:last").text());
alert($(".parent2").find("[class^=child]:last").text());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="parent1">
<div class="child1">Child1</div>
<div class="child2">Child2</div>
<div class="child3">Child3</div>
</div>
<div class="parent2">
<div class="child1">Child1</div>
<div class="child2">Child2</div>
</div>
console.log(Boolean($(".parent1").find(".child3").length));
console.log(Boolean($(".parent2").find(".child3").length));
console.log(!!$(".parent1").find(".child3").length);
console.log(!!$(".parent2").find(".child3").length);
console.log($(".parent1").find(".child3").length > 0);
console.log($(".parent2").find(".child3").length > 0);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="parent1">
<div class="child3"></div>
</div>
<div class="parent2"></div>
相关文章:
- 遍历DOM查找字符串有时会正确返回
- 查找大写字母的简单正则表达式是返回小写字母
- 在从索引位置返回的字符串中查找空白
- 在 jquery 选项卡中查找 span 标记并返回该索引
- 解析查询查找方法返回对象而不是数组
- _.数组中的每个查找值都返回 true 或 false.使用下划线 JS
- 通过getElementById查找所选索引是't返回正确的值
- navigator.plugins如何查找并返回安装在web浏览器上的插件对象数组
- 如何在日志文件中查找字符串并返回整行
- 正则表达式查找字符串并返回大括号之间的所有内容
- 使用'父母'和'查找'返回undefined-Jquery
- VBScript在目录中查找用户;返回结果(已找到或未找到)
- 如何在数组中查找重复值并返回 True 或 False
- Javascript - 从数组中查找所有类,从类中获取并返回内部文本
- eBay 查找 API findItemsByKeywords 返回“输入 URL 为标头 X-EBAY-SOA-OPE
- CanJS 模型查找全部返回重复项的列表
- JS regex.exec() 返回第一个查找
- 角度:通过数组查找其索引返回对象的内容
- 查找主干集合上的位置仅返回一个结果
- 按值查找嵌套对象并返回查找到的对象键