How to determine if an element exists with JQuery

藏色散人
Release: 2018-12-14 17:25:11
Original
14958 people have browsed it

JQuery determines whether an element exists. We can use the jQuery .length property to achieve this. When an event is triggered when a specific element exists in the DOM, you can use the jQuery .length property to determine whether the element exists.

How to determine if an element exists with JQuery

Below we will combine a simple code example to introduce to you the specific method ofJQuery to determine whether an element exists.

The code example is as follows:

    JQuery判断元素是否存在的示例   

点击下面的按钮检查元素

Copy after login

In the above code, we added a click event to the button. When the button button is clicked, the method of judging the element will be triggered. If the specified div exists, it will A prompt of "Element already exists" pops up, otherwise a prompt of "Element does not exist" pops up.

The judgment result is as follows:

When there is a div, click the button to display the following picture:

How to determine if an element exists with JQuery

How to determine if an element exists with JQuery

When the div does not exist, click the button to display the following image:

How to determine if an element exists with JQuery

Note: The length attribute contains the number of elements in the jQuery object.

This article is an introduction to JQuery to determine whether an element exists. It is also very simple. I hope it will be helpful to friends in need!

The above is the detailed content of How to determine if an element exists with JQuery. For more information, please follow other related articles on the PHP Chinese website!

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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!