jquery check if an object exists

 jQuery code to check if an object exists:

***********************mistake***********************

if($("#id")){}else{} because $("#id") returns object regardless of whether the object exists.

***********************correct***********************

if($("#id").length>0){}else{} uses the property length of the jQuery object to determine if > 0 exists.

or

 if($("#id")[0]){} else {}

or

Use native Javascript code to judge directly: if(document.getElementById("id")){} else {}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326777232&siteId=291194637