Home > Web Front-end > JS Tutorial > How to determine if an element has a certain attribute in jquery

How to determine if an element has a certain attribute in jquery

coldplay.xixi
Release: 2023-01-04 09:38:04
Original
3065 people have browsed it

Jquery method to determine whether an element has a certain attribute: First use the div tag to create a line of text and set its id to test; then within the function, obtain the div object through the element name and use [attr()] The method obtains the id attribute, and then uses the if statement to determine whether the obtained id value is undefined.

How to determine if an element has a certain attribute in jquery

The operating environment of this tutorial: windows7 system, jquery3.2.1 version, DELL G3 computer.

Recommended: jquery video tutorial

Jquery method to determine whether an element has a certain attribute:

1. Create a new one html file, named test.html, is used to explain jquery's judgment of whether an element has a certain attribute.

How to determine if an element has a certain attribute in jquery

2. In the test.html file, introduce the jquery.min.js library file and successfully load the file before you can use the methods in jquery.

How to determine if an element has a certain attribute in jquery

3. In the test.html file, use the div tag to create a line of text and set its id to test. It is mainly used to determine whether the div has this attribute.

How to determine if an element has a certain attribute in jquery

4. In the test.html file, use the button tag to create a button with the button name "Judge id attribute".

How to determine if an element has a certain attribute in jquery

5. In the test.html file, bind the onclick event to the button button. When the button is clicked, the pan() function is executed.

How to determine if an element has a certain attribute in jquery

6. In the pan() function, obtain the div object through the div element name, use the attr() method to obtain the id attribute, and then use the if statement to determine the obtained id value. Whether it is undefined (that is, there is no id). If the id attribute exists, it will prompt to output the id attribute. Otherwise, it will prompt "there is no id attribute".

How to determine if an element has a certain attribute in jquery

7. Open the test.html file in the browser and click the button to see the effect.

How to determine if an element has a certain attribute in jquery

Related free learning recommendations: javascript(Video)

The above is the detailed content of How to determine if an element has a certain attribute in 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template