jQuery 如何检查一个元素是否存在于另一个元素中?

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

How to check if an element exist into another element?

jquery

提问by markzzz

I'd like, to jQuery, understand if an element exist into another element.

对于 jQuery,我想了解一个元素是否存在于另一个元素中。

Somethings like :

类似的东西:

if($('#container').find('.search_element'))

must return YES if .search_elementis into #container, NO otherwise.

如果.search_element进入#container,则必须返回 YES ,否则返回NO。

How can I do it? Tried with $.contains('#container', '.search_element') but seems that this function doesnt works...

我该怎么做?尝试使用 $.contains('#container', '.search_element') 但似乎这个函数不起作用......

采纳答案by SpYk3HH



???UPDATE!!!

???更新!!!

See my answer [HERE]for a MUCHmore robust plugin!

见我的回答[这里]MUCH更强大的插件!



You could easily shorten @Chris answer with:

您可以通过以下方式轻松缩短@Chris 的答案:

if($("#container .search_element").length) { ...

You could also do the same with a useful plugin i made for this:

您也可以使用我为此制作的有用插件执行相同操作:

jsFiddle

js小提琴

(function($) {
    if (!$.exist) {
        $.extend({
            exist: function(elm) {
                if (typeof elm == null) return false;
                if (typeof elm != "object") elm = $(elm);
                return elm.length ? true : false;
            }
        });
        $.fn.extend({
            exist: function() {
                return $.exist($(this));
            }
        });
    }
})(jQuery);

USE

//  With your if statement
if($("#container .search_element").exist()) { ...

//  With ID 
$.exist("#eleID");
//  OR
$("#eleID").exist();

//  With class name
$.exist(".class-name");
//  OR
$(".class-name").exist();

//  With just tag // prolly not best idea aS there will be other tags on site
$.exist("div");
//  OR
$("div").exist();

Of course this plugin could be further extended to be much more fancy (handling multiple calls at once, creating non-existing elements based on a pram), but as it stand now, it does a very simple, very needed function ... Does this element exist? return Trueor False

当然,这个插件可以进一步扩展为更花哨的(一次处理多个调用,基于婴儿车创建不存在的元素),但就目前而言,它做了一个非常简单、非常需要的功能......这个元素存在吗?返回TrueFalse

jsFiddle

js小提琴

回答by Chris

A simple length check:

一个简单的长度检查:

if($("#container").find(".search_element").length) {
    // If the length is greater than 0, it exists
} else {
    // else, false
}

回答by karim79

You can use .isand :has, just to be complete. I would prefer a solution which tests .lengththough.

您可以使用.isand :has,只是为了完成。我更喜欢一个可以测试的解决方案.length

if($('#container').is(':has(.search_element)')) {
   // I haz that
}

回答by red-X

check the length of the array

检查数组的长度

if($('#container').find('.search_element').length)

回答by Roi

jQuery has a default method for this:

jQuery 有一个默认方法:

$("#container").has(".selector");

http://api.jquery.com/has/

http://api.jquery.com/has/