If the following jquery code is used to determine whether an object exists, it cannot be used.
if($("#id")){ //... }else{ //... }
Because $("#id") will return object regardless of whether the object exists.
Correct useTo determine whether the object existsYou should use:
if($("#id").length>0){ //... }else{ //... }
Use the jQuery object's property length to determine if > 0 it exists.
or
if($("#id")[0]){ //... }else{ //... }
Or directly use the native Javascript code to judge:
if(document.getElementById("id")){ //...}else{ //...}
The above is the detailed content of An example tutorial on determining whether an object exists in jQuery. For more information, please follow other related articles on the PHP Chinese website!