0){alert('object exists');}else{ alert('Object does not exist');}]."/> 0){alert('object exists');}else{ alert('Object does not exist');}].">

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

How jQuery determines whether the object with the specified id exists

coldplay.xixi
coldplay.xixiOriginal
2020-12-03 10:01:202273browse

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('对象不存在');
}

Or directly use the native Javascript code to judge:

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

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!

Statement:
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