如何检查选择器是否匹配jQuery中的内容?(复制)

时间:2021-11-27 07:40:47

This question already has an answer here:

这个问题已经有了答案:

In Mootools, I'd just run if ($('target')) { ... }. Does if ($('#target')) { ... } in jQuery work the same way?

在Mootools中,我只运行if ($('target')){…}。执行if ($('#target')){…在jQuery中也是这样工作的吗?

11 个解决方案

#1


667  

As the other commenters are suggesting the most efficient way to do it seems to be:

正如其他评论者提出的,最有效的方法似乎是:

if ($(selector).length ) {
    // Do something
}

If you absolutely must have an exists() function - which will be slower- you can do:

如果您一定要有一个exist()函数—它会比较慢—您可以这样做:

jQuery.fn.exists = function(){return this.length>0;}

Then in your code you can use

然后在代码中使用

if ($(selector).exists()) {
    // Do something
}

As answered here

在这里回答

#2


75  

no, jquery always returns a jquery object regardless if a selector was matched or not. You need to use .length

不,jquery总是返回jquery对象,不管选择器是否匹配。你需要用。length

if ( $('#someDiv').length ){

}

#3


19  

if you used:

如果你使用:

jQuery.fn.exists = function(){return ($(this).length > 0);}
if ($(selector).exists()) { }

you would imply that chaining was possible when it is not.

您可能会暗示,链接是可能的,但事实并非如此。

This would be better

这将是更好的

jQuery.exists = function(selector) {return ($(selector).length > 0);}
if ($.exists(selector)) { }

#4


15  

Yet another way:

另一种方法:

$('#elem').each(function(){
  // do stuff
});

#5


15  

I think most of the people replying here didn't quite understand the question, or else I might be mistaken.

我想大多数回答这个问题的人都不太明白这个问题,否则我可能弄错了。

The question is "how to check whether or not a selector exists in jQuery."

问题是“如何检查jQuery中是否存在选择器”。

Most people have taken this for "how to check whether an element exists in the DOM using jQuery." Hardly interchangeable.

大多数人认为这是“如何使用jQuery检查DOM中是否存在元素”。几乎可以互换。

jQuery allows you to create custom selectors, but see here what happens when you try to use on e before initializing it;

jQuery允许您创建自定义选择器,但是看看在初始化e之前尝试在e上使用时会发生什么;

$(':YEAH');
"Syntax error, unrecognized expression: YEAH"

After running into this, I realized it was simply a matter of checking

在遇到这个问题后,我意识到这只是一个检查的问题

if ($.expr[':']['YEAH']) {
    // Query for your :YEAH selector with ease of mind.
}

Cheers.

欢呼。

#6


13  

if ($('#elem')[0]) {
  // do stuff
}

#7


3  

Alternatively:

另外:

if( jQuery('#elem').get(0) ) {}

#8


1  

I prefer the

我更喜欢

    if (jQuery("#anyElement").is("*")){...}

Which basically checks if this elements is a kind of "*" (any element). Just a cleaner syntax and the "is" makes more sense inside an "if"

它主要检查这个元素是否是一种“*”(任何元素)。更简洁的语法"is"在if中更有意义

#9


1  

jQuery.fn.exists = function(selector, callback) {
    var $this = $(this);
    $this.each(function() {
        callback.call(this, ($(this).find(selector).length > 0));
    });
};

#10


-3  

For me .exists doesn't work, so I use the index :

对我来说,存在不起作用,所以我使用索引:

if ($("#elem").index() ! = -1) {}

#11


-6  

firstly create a function:

首先创建一个函数:

$.fn.is_exists = function(){ return document.getElementById(selector) }

then

然后

if($(selector).is_exists()){ ... }

#1


667  

As the other commenters are suggesting the most efficient way to do it seems to be:

正如其他评论者提出的,最有效的方法似乎是:

if ($(selector).length ) {
    // Do something
}

If you absolutely must have an exists() function - which will be slower- you can do:

如果您一定要有一个exist()函数—它会比较慢—您可以这样做:

jQuery.fn.exists = function(){return this.length>0;}

Then in your code you can use

然后在代码中使用

if ($(selector).exists()) {
    // Do something
}

As answered here

在这里回答

#2


75  

no, jquery always returns a jquery object regardless if a selector was matched or not. You need to use .length

不,jquery总是返回jquery对象,不管选择器是否匹配。你需要用。length

if ( $('#someDiv').length ){

}

#3


19  

if you used:

如果你使用:

jQuery.fn.exists = function(){return ($(this).length > 0);}
if ($(selector).exists()) { }

you would imply that chaining was possible when it is not.

您可能会暗示,链接是可能的,但事实并非如此。

This would be better

这将是更好的

jQuery.exists = function(selector) {return ($(selector).length > 0);}
if ($.exists(selector)) { }

#4


15  

Yet another way:

另一种方法:

$('#elem').each(function(){
  // do stuff
});

#5


15  

I think most of the people replying here didn't quite understand the question, or else I might be mistaken.

我想大多数回答这个问题的人都不太明白这个问题,否则我可能弄错了。

The question is "how to check whether or not a selector exists in jQuery."

问题是“如何检查jQuery中是否存在选择器”。

Most people have taken this for "how to check whether an element exists in the DOM using jQuery." Hardly interchangeable.

大多数人认为这是“如何使用jQuery检查DOM中是否存在元素”。几乎可以互换。

jQuery allows you to create custom selectors, but see here what happens when you try to use on e before initializing it;

jQuery允许您创建自定义选择器,但是看看在初始化e之前尝试在e上使用时会发生什么;

$(':YEAH');
"Syntax error, unrecognized expression: YEAH"

After running into this, I realized it was simply a matter of checking

在遇到这个问题后,我意识到这只是一个检查的问题

if ($.expr[':']['YEAH']) {
    // Query for your :YEAH selector with ease of mind.
}

Cheers.

欢呼。

#6


13  

if ($('#elem')[0]) {
  // do stuff
}

#7


3  

Alternatively:

另外:

if( jQuery('#elem').get(0) ) {}

#8


1  

I prefer the

我更喜欢

    if (jQuery("#anyElement").is("*")){...}

Which basically checks if this elements is a kind of "*" (any element). Just a cleaner syntax and the "is" makes more sense inside an "if"

它主要检查这个元素是否是一种“*”(任何元素)。更简洁的语法"is"在if中更有意义

#9


1  

jQuery.fn.exists = function(selector, callback) {
    var $this = $(this);
    $this.each(function() {
        callback.call(this, ($(this).find(selector).length > 0));
    });
};

#10


-3  

For me .exists doesn't work, so I use the index :

对我来说,存在不起作用,所以我使用索引:

if ($("#elem").index() ! = -1) {}

#11


-6  

firstly create a function:

首先创建一个函数:

$.fn.is_exists = function(){ return document.getElementById(selector) }

then

然后

if($(selector).is_exists()){ ... }