检查元素是否包含 jQuery 中的另一个元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13182873/
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-26 12:23:07  来源:igfitidea点击:

Check if element contains another one in jQuery

jqueryjquery-selectors

提问by Jeff B.

I found many related things on Stack Overflow but not applicable for my case.

我在 Stack Overflow 上发现了很多相关的东西,但不适用于我的情况。

It's all in this exemple, I need to check if an element contains another one, and if yes, append something.

这一切都在这个例子中,我需要检查一个元素是否包含另一个元素,如果是,则附加一些内容。

$(".bt_repondre").click(function(){
    comment = $(this).parent().parent().parent();
    //I want to check if comment contains a .comment_full element, if no, append.
    comment.append('add');
});

Hope you can help me, I tried so many things...

希望你能帮助我,我尝试了很多东西......

采纳答案by Nelson

Just use .find()and check if returns an element, like this:

只需使用.find()并检查是否返回一个元素,如下所示:

$(".bt_repondre").click(function(){
    comment = $(this).parent().parent().parent();
    if (! comment.find('.comment_full').length) {
       comment.append('add');
    }
});

回答by Blender

You can use .hasand .length:

您可以使用.has.length

if (comment.has('.comment_full').length) {
    // It has that element
}
  • .findwill iterate over all of the descendants, but .haswill stop once a descendant that matches the selector has been found. It might run faster.

  • .lengthjust checks to see if the length of the resulting set of elements is non-zero.

  • .find将迭代所有后代,但.has一旦找到与选择器匹配的后代,就会停止。它可能运行得更快。

  • .length只是检查结果元素集的长度是否非零。

回答by niftylettuce

Most of these answers are incorrect. You mustpass a DOM node not a jQuery element for $.contains to work properly per https://api.jquery.com/jQuery.contains/.

大多数这些答案是不正确的。您必须传递一个 DOM 节点而不是 jQuery 元素,$.contains 才能根据https://api.jquery.com/jQuery.contains/正常工作。

For example, this is how you would determine if $('#a')contains $('#b).

例如,这是您确定是否$('#a')包含的方式$('#b)

HTML:

HTML:

<div id="a">
  <div id="b"></div>
</div>

JavaScript:

JavaScript:

var $a = $('#a');
var $b = $('#b');
var contains = $.contains($a.get(0), $b.get(0));
console.log('contains', contains);
// outputs `true`

回答by Dovlet Mamenov

If you were looking let's say any tr element inside table with id of myTable you can use following code:

如果您正在寻找让我们说表中任何带有 myTable id 的 tr 元素,您可以使用以下代码:

if($('#productsTableBody tr').length)
{
//...
}

This way we can check wheather table contains any row.

这样我们就可以检查表是否包含任何行。

回答by ksoo

Most of these answers are great, but there's a new method called contains(container,contained)(added in 1.4) that returns a Boolean! It does basically the same thing as Blender's code snippet but is probably quicker, both to type and to execute.

这些答案中的大多数都很棒,但是有一个名为contains(container,contained)(在 1.4 中添加)的新方法返回一个Boolean! 它的作用与 Blender 的代码片段基本相同,但可能更快,无论是输入还是执行。

Implemented in your code, it'd look like this:

在您的代码中实现,它看起来像这样:

$(".bt_repondre").click(function(){
    comment = $(this).parent().parent().parent();
    if(!$.contains(comment, $('.comment_full')))//same as if(!jQuery.contains(...
    {
        comment.append('add');
    }
});

回答by YogeshWaran

try this

尝试这个

 comment.has('a.comment_full').length == 0