jQuery 如果存在两个元素之一,请执行某些操作

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

If one of two elements exists, do something

jqueryexists

提问by eozzy

I currently do this to check if one of two elements exists:

我目前这样做是为了检查是否存在两个元素之一:

if ($(".element1").length > 0 || $(".element2").length > 0) {
  //do stuff...
}

Is there a better way to rewrite the same? I mean, is .lengththe same as .length > 0?

有没有更好的方法来重写它?我的意思是,是.length一样的.length > 0吗?

采纳答案by eozzy

if ( $('#myDiv')[0] ) { //do something }

..works best!

..效果最好!

Found here.

这里找到。

回答by micahwittman

if ($(".element1").is('*') || $(".element2").is('*')) {
    // code
}

EDIT(per comment) Select elements by multiple classes in one call:

编辑(每条评论)在一次调用中按多个类选择元素:

if ($(".element1, .element2").is('*')) {
    // code
}

回答by Aaron

All jQuery elements have the .lengthproperty. You can just go:

所有 jQuery 元素都具有该.length属性。你可以去:

if ($('img').length) // Implies: If this element exists..

http://jqueryfordesigners.com/element-exists/

http://jqueryfordesigners.com/element-exists/

回答by bharadwaj

!$.isEmptyObject($.find('#urId'))

this will return "true" if the element exists and False if not

如果元素存在,则返回“true”,否则返回 False

cheers :)

欢呼:)

回答by Adriano Pedro

Just use .each().

只需使用 .each()。

$(".element1").each(function(){
  //Do Something here
}

Simple...

简单的...

回答by user3336882

$.fn.exists = function(ifExists) {
    return this.length ? ifExists.call(this, this) : this;
};

usage:

用法:

$('.element1, .element2').exists(function(els) {
    // this and els refers to $('.element1, .element2')
});

回答by SpYk3HH



See Extremely updated version of this plugin here! Now uses callback function so you can maintain chainability if you choose. Can completely replace if statementor can still be used within if statement

在此处查看此插件的极度更新版本!现在使用回调函数,因此您可以选择保持可链接性。可以完全替换if 语句或仍然可以在if 语句中使用



You could create a very simple jQuery plug-in for this, as such:

您可以为此创建一个非常简单的 jQuery 插件,如下所示:

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 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();

With your If statement

用你的 If 语句

if ($(".element1").exist() || $(".element2").exist()) {
    ...stuff...
}

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小提琴