Home > Web Front-end > JS Tutorial > How to determine whether an object exists in jquery

How to determine whether an object exists in jquery

coldplay.xixi
Release: 2020-11-30 14:10:04
Original
3125 people have browsed it

Jquery method to determine whether an object exists: 1. Use traditional Javascript writing method, the code is [obj = document.getElementById("someID")]; 2. Use jQuery to determine the object. If it is greater than 0, identify the id. exist.

How to determine whether an object exists in jquery

The operating environment of this tutorial: windows10, jquery2.2.4, this article is applicable to all brands of computers.

How jquery determines whether an object exists:

1. Traditional Javascript writing method

obj = document.getElementById("someID"); 
if (obj){ 
   obj.innerText("hi"); 
}
Copy after login

In jQuery, var obj = $ ("#id")No matter whether the id control exists or not, object is returned, so if(obj) cannot be used to determine whether the control exists

2. jQuery determines whether the object exists

Method 1:

if ($('#target_obj_id').length > 0) {
//如果大于0 标识 id 为target_obj_id的对象存在,否则不存在 
   //对象存在的处理逻辑 
} else { 
   //对象不存在的处理逻辑 
}
Copy after login

Method 2:

if ($('#target_obj_id')[0]) { 
  //对象存在的处理逻辑 
} else { 
  //对象不存在的处理逻辑 
}
Copy after login

Related free learning recommendations: JavaScript (video)

The above is the detailed content of How to determine whether an object exists in jquery. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template