The simplest way is to determine the matching length of the element
For example, HTML code:
Usually our approach is
if($('.mydiv ').length>0)
The more reliable and error-free method is:
if($('.mydiv').length && $('.mydiv').length>0)
return true;
Use the traditional javascript method, as follows:
if( document.getElementById('div')) {
Using jQuery is relatively simple. You only need to determine whether the length of this element is 0. If it is 0, this element does not exist. The code is as follows:
Copy code
can even find the combined element. As follows, we find whether an element whose id is defined as div contains img. The code is as follows:
Copy code
Isn’t it very simple? You can try it