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: 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');}].
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!