JQuery determines whether an object exists. jquery determines whether an object exists.
This example describes how jQuery checks whether an object exists. Share it with you for your reference. The details are as follows:
I. Traditional Javascript writing
Obj = document. getElementById ("someID"); if (obj) {obj. innerText ("hi ");}
In jQuery, var obj = $ ("# id") returns an object regardless of whether the control id exists. In this way, if (obj) cannot be used to determine whether the control exists.
Ii. jQuery checks whether an object exists
Method 1:
If ($ ('# target_obj_id '). length> 0) {// if an object whose id is target_obj_id is greater than 0 exists, otherwise the processing logic of the object does not exist} else {// processing logic of the object does not exist}
Method 2:
If ($ ('# target_obj_id') [0]) {// processing logic of the object} else {// processing logic of the object that does not exist}
I hope this article will help you with jQuery programming.