Javascript 如何检查 jQuery find 返回 true 还是 false?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27646559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:28:09  来源:igfitidea点击:

How to check jQuery find return true or false?

javascriptjquery

提问by Sadikhasan

HTML Code

HTML代码

<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>

jQuery Code

jQuery 代码

alert($(".parent1").find(".child3").text());
alert($(".parent2").find(".child3").text());

My question is how to check findfunction return true or false?

我的问题是如何检查find函数返回真还是假?

In above code it return blank value, where parent1class have child3class where parent2class do not have child3class.

在上面的代码中,它返回空值,其中parent1类有child3类,而parent2类没有child3类。

JS Fiddle

JS小提琴

回答by Amit Joki

You couldn't use just findin ifcondition. You could use hasor check for the lengthproperty.

你不能只是findif条件下使用。您可以使用has或检查该length属性。

var elm = $('.parent1');
if(elm.has('.child3')){
   var child3 = elm.find('.child3');
}

Or simply like this

或者只是像这样

var child3 = $('.parent1').find(".child3");
if(child3.length > 0) {
  // child3 is present
}

回答by Satpal

You can use .lengthproperty to check that element exists.

您可以使用.length属性来检查元素是否存在。

var elem = $(".parent2").find(".child3");
if(elem.length){
    alert(elem.text());
}else{
    alert('Element doesnt exists')
}

Alternatively, You can use .has()

或者,您可以使用 .has()

var elem = $(".parent2");
if(elem.has(".child3")){
    alert(elem.find(".child3").text());
}else{
    alert('Element doesnt exists')
}

回答by charlietfl

Whenever you wrap a selector in $()it will always return a jQuery object that contains the array of matching elements.

每当您将选择器包装在$()其中时,它将始终返回一个包含匹配元素数组的 jQuery 对象。

Therefore you can use the lengthproperty to test if there are matches

因此,您可以使用该length属性来测试是否有匹配项

var $collection = $(".parent2").find(".child3").length;
if ($collection.length)....

You can use other approaches also such as is()

您也可以使用其他方法,例如 is()

var #collection = $(".parent2");
if($collection.children().is(".child3") /* returns tru if there are matches

回答by chrki

As the docs for find()explain, find()returns a jQuery object. It has a length propery which can be checked for a successful query.

作为解释的文档find()find()返回一个 jQuery 对象。它有一个长度属性,可以检查查询是否成功。

if($(".parent1").find(".child3").length > 0) {
    alert("parent1 child3");
}
if($(".parent2").find(".child3").length > 0) {
    alert("parent2 child3");
}

回答by Alexander T.

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>

回答by Alexander T.

There is no element .child3 for .parent2,

.parent2 没有元素 .child3,

alert($(".parent2").find(".child3").text());

Try this:

尝试这个:

alert($(".parent2").find(".child2").text());

Try this if you want only last item

如果你只想要最后一项,试试这个

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>