Home > Web Front-end > JS Tutorial > body text

jQuery's reliable method to determine whether an element exists_jquery

WBOY
Release: 2016-05-16 16:49:35
Original
1009 people have browsed it

The simplest way is to determine the matching length of the element

For example, HTML code:

Copy code The code is as follows:

Usually our approach is

Copy code The code is as follows:
if($('.mydiv ').length>0)

The more reliable and error-free method is:

Copy the code The code is as follows:

if($('.mydiv').length && $('.mydiv').length>0)
return true;

Use the traditional javascript method, as follows:

Copy the code The code is 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
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template