Home > Web Front-end > Front-end Q&A > How to determine whether jquery contains an id element

How to determine whether jquery contains an id element

WBOY
Release: 2022-01-14 10:57:12
Original
3810 people have browsed it

Method: 1. Use the attr() method to obtain the id attribute value of the specified element, the syntax is "$(element).attr('id')"; 2. Use the if statement and the "==" operation Just use the symbol to determine whether it contains an id. The syntax is "if (id attribute value object == specified id value) {contains code} else {does not contain code}".

How to determine whether jquery contains an id element

The operating environment of this tutorial: windows10 system, jquery3.2.1 version, Dell G3 computer.

How jquery determines whether it contains an id element

In jquery, you can obtain the element object and use the attr() method to obtain the id attribute value to determine whether the element has a certain id. The following is an example of how jquery determines whether an element has a certain id.

The example is as follows:

Create a new html file and name it test.html, which is used to explain jquery's judgment of whether an element has a certain id. Use the div tag to create a line of text, and set the div's id to tsdiv, which will be used to determine whether the id exists using the jquery method below. Use the button tag to create a button with the button name "Judge element id". Bind the onclick click event to the button button. When the button is clicked, the panduan() function is executed.

How to determine whether jquery contains an id element

In the js tag, create the panduan() function. Within the function, obtain the div object through the element name (div), and use the attr() method to obtain the attribute value of the id. , save the value in the id variable.

In the panduan() function, use the if statement to determine whether the id obtained in the previous step is equal to tsdiv. If they are equal, it will prompt "id exists, and the id attribute value is: xxx". Otherwise, it will prompt "id does not exist." Attributes".

How to determine whether jquery contains an id element

Output results:

How to determine whether jquery contains an id element

Recommended related video tutorials: jQuery video tutorial

The above is the detailed content of How to determine whether jquery contains an id element. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template