Home > Web Front-end > JS Tutorial > How jQuery determines whether the object with the specified id exists

How jQuery determines whether the object with the specified id exists

coldplay.xixi
Release: 2023-01-04 09:36:27
Original
2332 people have browsed it

How jQuery determines whether the object with the specified id exists: determine whether the length of the object is greater than 0. The code is [if($("selector").length>0){alert('The object exists ');}else{alert('Object does not exist');}].

How jQuery determines whether the object with the specified id exists

The operating environment of this tutorial: windows7 system, jquery3.2.1 version, thinkpad t480 computer.

How jQuery determines whether the object with the specified id exists:

jQuery determines whether the object with the specified id exists. You only need to determine whether the length of the object is greater than 0.

Example:

The correct way to judge is as follows:

if($("#object_id").length>0)
{
  alert('对象存在');
}
else
{
  alert('对象不存在');
}
Copy after login

Or directly use the native Javascript code to judge:

if(document.getElementById("id"))
{
alert('对象存在');
}
 else
{
alert('对象不存在');
}
Copy after login

Related learning recommendations :javascript video tutorial

The above is the detailed content of How jQuery determines whether the object with the specified id exists. 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